lib/ember-cli/app.rb in ember-cli-rails-0.3.5 vs lib/ember-cli/app.rb in ember-cli-rails-0.4.0

- old
+ new

@@ -1,43 +1,62 @@ require "timeout" +require "ember-cli/html_page" +require "ember-cli/asset_resolver" module EmberCLI class App - ADDON_VERSION = "0.0.12" + ADDON_VERSION = "0.0.13" EMBER_CLI_VERSIONS = [ "~> 0.1.5", "~> 0.2.0", "~> 1.13" ] class BuildError < StandardError; end attr_reader :name, :options, :paths, :pid delegate :root, to: :paths - def initialize(name, options={}) + def initialize(name, **options) @name, @options = name.to_s, options @paths = PathSet.new(self) end def compile @compiled ||= begin prepare silence_build{ exec command } check_for_build_error! + copy_index_html_file true end end def install_dependencies - exec "#{bundler_path} install" if gemfile_path.exist? - exec "#{npm_path} install" + if gemfile_path.exist? + exec "#{bundler_path} install" + end + + exec "#{npm_path} prune && #{npm_path} install" + + if bower_path.nil? + fail <<-FAIL + Bower is required by EmberCLI. + + Install it with: + + $ npm install -g bower + FAIL + else + exec "#{bower_path} prune && #{bower_path} install" + end end def run prepare FileUtils.touch lockfile_path cmd = command(watch: true) @pid = exec(cmd, method: :spawn) Process.detach pid + copy_index_html_file set_on_exit_callback end def run_tests prepare @@ -47,18 +66,38 @@ def stop Process.kill :INT, pid if pid @pid = nil end + def index_html(sprockets:, head:, body:) + asset_resolver = AssetResolver.new( + app: self, + sprockets: sprockets, + ) + html_page = HtmlPage.new( + asset_resolver: asset_resolver, + content: index_file.read, + head: head, + body: body, + ) + + html_page.render + end + def exposed_js_assets - %W[#{name}/vendor #{name}/#{ember_app_name}] + [vendor_assets, application_assets] end + alias exposed_css_assets exposed_js_assets - def exposed_css_assets - %W[#{name}/vendor #{name}/#{ember_app_name}] + def vendor_assets + "#{name}/vendor" end + def application_assets + "#{name}/#{ember_app_name}" + end + def wait Timeout.timeout(build_timeout) do wait_for_build_complete_or_error end rescue Timeout::Error @@ -148,10 +187,11 @@ fail error end def prepare @prepared ||= begin + check_dependencies! check_addon! check_ember_cli_version! reset_build_error! symlink_to_assets_root add_assets_to_precompile_list @@ -187,29 +227,64 @@ in your Ember application root: #{root} MSG end end + def check_dependencies! + unless node_modules_present? + fail <<-MSG.strip_heredoc + EmberCLI app dependencies are not installed. From your Rails application root please run: + + $ bundle exec rake ember:install + + If you do not require Ember at this URL, you can restrict this check using the `enable` + option in the EmberCLI initializer. + MSG + end + end + + def assets_path + paths.assets.join(name) + end + + def copy_index_html_file + if environment == "production" + FileUtils.cp(assets_path.join("index.html"), index_file) + end + end + + def index_file + if environment == "production" + applications_path.join("#{name}.html") + else + dist_path.join("index.html") + end + end + def symlink_to_assets_root - assets_path.join(name).make_symlink dist_path.join("assets") + assets_path.make_symlink dist_path.join("assets") rescue Errno::EEXIST # Sometimes happens when starting multiple Unicorn workers. # Ignoring... end def add_assets_to_precompile_list Rails.configuration.assets.precompile << /\A#{name}\// end - def command(options={}) - watch = "" - if options[:watch] - watch = "--watch" - watch += " --watcher #{watcher}" if watcher + def command(watch: false) + watch_flag = "" + + if watch + watch_flag = "--watch" + + if watcher + watch_flag += " --watcher #{watcher}" + end end - "#{ember_path} build #{watch} --environment #{environment} --output-path #{dist_path} #{log_pipe}" + "#{ember_path} build #{watch_flag} --environment #{environment} --output-path #{dist_path} #{log_pipe}" end def log_pipe "| #{tee_path} -a #{log_path}" if tee_path end @@ -221,39 +296,51 @@ def environment EmberCLI.env.production?? "production" : "development" end def package_json - @package_json ||= JSON.parse(package_json_file_path.read).with_indifferent_access + @package_json ||= + JSON.parse(package_json_file_path.read).with_indifferent_access end + def addon_package_json + @addon_package_json ||= + JSON.parse(addon_package_json_file_path.read).with_indifferent_access + end + + def addon_version + addon_package_json.fetch("version") + end + def dev_dependencies package_json.fetch("devDependencies", {}) end def addon_present? - dev_dependencies["ember-cli-rails-addon"] == ADDON_VERSION && - addon_package_json_file_path.exist? + addon_package_json_file_path.exist? && + addon_version == ADDON_VERSION end + def node_modules_present? + node_modules_path.exist? + end + def excluded_ember_deps Array.wrap(options[:exclude_ember_deps]).join(?,) end def env_hash - ENV.clone.tap do |vars| - vars.store "RAILS_ENV", Rails.env - vars.store "DISABLE_FINGERPRINTING", "true" - vars.store "EXCLUDE_EMBER_ASSETS", excluded_ember_deps - vars.store "BUNDLE_GEMFILE", gemfile_path.to_s if gemfile_path.exist? + ENV.to_h.tap do |vars| + vars["RAILS_ENV"] = Rails.env + vars["DISABLE_FINGERPRINTING"] = "true" + vars["EXCLUDE_EMBER_ASSETS"] = excluded_ember_deps + vars["BUNDLE_GEMFILE"] = gemfile_path.to_s if gemfile_path.exist? end end - def exec(cmd, options={}) - method_name = options.fetch(:method, :system) - + def exec(cmd, method: :system) Dir.chdir root do - Kernel.public_send(method_name, env_hash, cmd, err: :out) + Kernel.public_send(method, env_hash, cmd, err: :out) end end def wait_for_build_complete_or_error loop do