Class: ModJS::Blueprint

Inherits:
ArchitectureJS::Blueprint
  • Object
show all
Defined in:
lib/modjs-architecture.rb

Instance Method Summary collapse

Constructor Details

#initialize(config, root = nil) ⇒ Blueprint

Returns a new instance of Blueprint.



17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/modjs-architecture.rb', line 17

def initialize(config, root = nil)
  @config = {
    blueprint: 'modjs',
    src_dir: 'modules',
    build_dir: 'application',
    dependencies: [],
    autoload: []
  }
  @config.merge! config unless config.nil?

  super(@config, root)

  @directories = %w'application elements lib models modules plugins spec templates'
end

Instance Method Details

#compile_application_file(file) ⇒ Object



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/modjs-architecture.rb', line 129

def compile_application_file(file)
  sprockets = Sprockets::Secretary.new(
    root: ModJS::base_dir,
    asset_root: File.expand_path(@config[:asset_root], @root),
    load_path: ['repository', @root],
    source_files: [file],
    interpolate_constants: false
  )

  application_file = sprockets.concatenation
  application_file.save_to file
  sprockets.install_assets
rescue Exception => error
  @errors = true
  puts ArchitectureJS::Notification.error "Sprockets error: #{error.message}"
end

#compile_src_filesObject



76
77
78
79
80
81
# File 'lib/modjs-architecture.rb', line 76

def compile_src_files
  @src_files.each do |file_path|
    file_name = get_file_name file_path.gsub(/\.module\.js$/, '.js')
    compile_src_file file_path, file_name
  end
end

#copy_modjs_core_to_libObject



52
53
54
# File 'lib/modjs-architecture.rb', line 52

def copy_modjs_core_to_lib
  FileUtils.cp "#{ModJS::lib_dir}/mod.js", "#{@root}/lib/"
end

#copy_spec_filesObject



56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/modjs-architecture.rb', line 56

def copy_spec_files
  FileUtils.mkdir "#{@root}/spec/jasmine"
  FileUtils.cp "#{ModJS::base_dir}/spec/javascripts/application_spec.js", "#{@root}/spec/application_spec.js"
  FileUtils.cp "#{ModJS::base_dir}/spec/javascripts/dom_spec.js", "#{@root}/spec/dom_spec.js"
  FileUtils.cp "#{ModJS::base_dir}/spec/javascripts/existence_spec.js", "#{@root}/spec/existence_spec.js"
  FileUtils.cp "#{ModJS::base_dir}/spec/javascripts/module_spec.js", "#{@root}/spec/module_spec.js"
  FileUtils.cp "#{ModJS::base_dir}/lib/modjs-architecture/jasmine/jasmine-html.js", "#{@root}/spec/jasmine/"
  FileUtils.cp "#{ModJS::base_dir}/lib/modjs-architecture/jasmine/jasmine.css", "#{@root}/spec/jasmine/"
  FileUtils.cp "#{ModJS::base_dir}/lib/modjs-architecture/jasmine/jasmine.js", "#{@root}/spec/jasmine/"
  FileUtils.cp "#{ModJS::base_dir}/lib/modjs-architecture/jasmine/jasmine_favicon.png", "#{@root}/spec/jasmine/"
  FileUtils.cp "#{ModJS::base_dir}/lib/modjs-architecture/jasmine/MIT.LICENSE", "#{@root}/spec/jasmine/"
  FileUtils.cp "#{ModJS::base_dir}/lib/modjs-architecture/jasmine/index.html", "#{@root}/spec/jasmine/"
end

#createObject



32
33
34
35
36
37
# File 'lib/modjs-architecture.rb', line 32

def create
  super
  copy_modjs_core_to_lib
  copy_spec_files
  puts ArchitectureJS::Notification.added "#{@config[:build_dir]}/#{@config[:name].downcase}.js created"
end

#create_application_fileObject



46
47
48
49
50
# File 'lib/modjs-architecture.rb', line 46

def create_application_file
  app_file = "#{@root}/#{@config[:build_dir]}/#{@config[:name].downcase}.js"
  FileUtils.cp "#{ModJS::lib_dir}/mod.js", app_file
  File.open(app_file, 'a') { |f| f.write("\nvar #{@config[:name]} = new Mod.Application('#{@config[:name]}');") }
end

#get_file_name(module_path) ⇒ Object



39
40
41
42
43
44
# File 'lib/modjs-architecture.rb', line 39

def get_file_name(module_path)
  module_file = module_path.split(/[\\\/]/).last

  module_filename = module_file.gsub(/\.module\.js$/, '') if module_file.match /\.module\.js$/
  module_filename = module_file.gsub(/\.js$/, '')
end

#update(compress = false) ⇒ Object



70
71
72
73
74
# File 'lib/modjs-architecture.rb', line 70

def update(compress = false)
  read_config
  update_application_file
  super(compress)
end

#update_application_fileObject



83
84
85
86
87
88
89
90
# File 'lib/modjs-architecture.rb', line 83

def update_application_file
  tmp_file = write_temp_file
  app_file = "#{@root}/#{@config[:build_dir]}/#{@config[:name].downcase}.js"

  ArchitectureJS::Notification.log "#{app_file} updated"
  compile_application_file tmp_file
  FileUtils.mv tmp_file, app_file
end

#write_autoload(file) ⇒ Object



119
120
121
122
123
124
125
126
127
# File 'lib/modjs-architecture.rb', line 119

def write_autoload(file)
  if @config[:autoload]
    @config[:autoload].each do |autoload|
      file << "/*---------- ModJS autoload #{autoload} ----------*/\n"
      file << "//= require #{autoload}\n\n" if autoload.match(/^\<.+\>$/)
      file << "//= require \"#{autoload}\"\n\n" if autoload.match(/^[^\<].+|[^\>]$/)
    end
  end
end

#write_core(file) ⇒ Object



113
114
115
116
117
# File 'lib/modjs-architecture.rb', line 113

def write_core(file)
  file << "/*---------- ModJS ../lib/mod.js ----------*/\n"
  file << "//= require \"lib/mod.js\"\n\n"
  file << "var #{@config[:name]} = new Mod.Application('#{@config[:name]}');\n\n"
end

#write_dependencies(file) ⇒ Object



103
104
105
106
107
108
109
110
111
# File 'lib/modjs-architecture.rb', line 103

def write_dependencies(file)
  if @config[:dependencies]
    @config[:dependencies].each do |dependency|
      file << "/*---------- ModJS dependency #{dependency} ----------*/\n"
      file << "//= require #{dependency}\n\n" if dependency.match(/^\<.+\>$/)
      file << "//= require \"#{dependency}\"\n\n" if dependency.match(/^[^\<].+|[^\>]$/)
    end
  end
end

#write_temp_fileObject



92
93
94
95
96
97
98
99
100
101
# File 'lib/modjs-architecture.rb', line 92

def write_temp_file
  tmp_file = "#{@root}/#{@config[:name].downcase}.js"

  File.open(tmp_file, "w+") do |file|
    write_dependencies file
    write_core file
    write_autoload file
  end
  tmp_file
end