diff --git a/defaults/liftoffrc b/defaults/liftoffrc index fa364ce..6847db7 100644 --- a/defaults/liftoffrc +++ b/defaults/liftoffrc @@ -13,6 +13,7 @@ enable_static_analyzer: true indentation_level: 4 use_tabs: false use_cocoapods: true +enable_settings: true strict_prompts: false run_script_phases: diff --git a/lib/liftoff.rb b/lib/liftoff.rb index ff5e751..0e86f05 100644 --- a/lib/liftoff.rb +++ b/lib/liftoff.rb @@ -3,12 +3,14 @@ require 'yaml' require 'erb' require 'etc' +require 'find' require 'highline/import' require 'xcodeproj' require 'liftoff/cli' require 'liftoff/cocoapods_setup' +require 'liftoff/settings_generator' require 'liftoff/configuration_parser' require 'liftoff/deprecation_manager' require 'liftoff/file_manager' diff --git a/lib/liftoff/cli.rb b/lib/liftoff/cli.rb index 177eacd..c48acb6 100644 --- a/lib/liftoff/cli.rb +++ b/lib/liftoff/cli.rb @@ -46,6 +46,10 @@ def global_options @options[:xcode_command] = false end + opts.on('--[no-]settings', 'Enable/Disable Settings.bundle') do |enable_settings| + @options[:enable_settings] = enable_settings + end + opts.on('--template [TEMPLATE NAME]', 'Use the specified project template') do |template_name| @options[:project_template] = template_name end diff --git a/lib/liftoff/file_manager.rb b/lib/liftoff/file_manager.rb index bff31c4..0f611d2 100644 --- a/lib/liftoff/file_manager.rb +++ b/lib/liftoff/file_manager.rb @@ -8,13 +8,13 @@ def create_project_dir(name, &block) exit 1 end - def generate(template, destination = template, project_config = ProjectConfiguration.new({})) + def generate(template, destination = template, config = ProjectConfiguration.new({})) create_destination_path(destination) template_path = TemplateFinder.new.template_path(template) if template_is_directory?(template_path) - copy_template_directory(template_path, destination) + copy_template_directory(template_path, destination, config) else - create_template_file(destination, template_path, project_config) + create_template_file(destination, template_path, config) end end @@ -31,17 +31,23 @@ def template_contents(filename) private - def create_template_file(destination, template_path, project_config) + def create_template_file(destination, template_path, config) existing_content = existing_file_contents(destination) - move_template(template_path, destination, project_config) + move_template(template_path, destination, config) append_original_file_contents(destination, existing_content) if File.executable?(template_path) File.chmod(0755, destination) end end - def copy_template_directory(template, path) - FileUtils.cp_r(template, File.join(*path)) + def copy_template_directory(template, path, config) + destination = File.join(*path) + FileUtils.cp_r(template, destination) + Find.find(destination) do |file| + unless (File.directory?(file)) + move_template(file, file, config) + end + end end def existing_file_contents(filename) @@ -56,14 +62,18 @@ def create_destination_path(destination) FileUtils.mkdir_p(File.dirname(destination)) end - def move_template(template, destination, project_config) - rendered_template = StringRenderer.new(project_config).render(File.read(template)) + def move_template(template, destination, config) + rendered_template = render_template(template, config) File.open(destination, 'w') do |file| file.write(rendered_template) end end + def render_template(template, config) + StringRenderer.new(config).render(File.read(template)) + end + def append_original_file_contents(filename, original_contents) if original_contents File.open(filename, 'a') do |file| diff --git a/lib/liftoff/launchpad.rb b/lib/liftoff/launchpad.rb index d522863..d6370eb 100644 --- a/lib/liftoff/launchpad.rb +++ b/lib/liftoff/launchpad.rb @@ -15,6 +15,7 @@ def liftoff(options) generate_project setup_cocoapods generate_templates + generate_settings install_cocoapods perform_project_actions open_project @@ -62,6 +63,10 @@ def generate_project ProjectBuilder.new(@config).create_project end + def generate_settings + SettingsGenerator.new(@config).generate + end + def generate_git GitSetup.new(@config).setup end diff --git a/lib/liftoff/project_builder.rb b/lib/liftoff/project_builder.rb index f707ec5..d7faabe 100644 --- a/lib/liftoff/project_builder.rb +++ b/lib/liftoff/project_builder.rb @@ -88,7 +88,7 @@ def linkable_file?(name) end def resource_file?(name) - name.end_with?('xcassets') + name.end_with?('xcassets', 'bundle') end def template_file?(object) diff --git a/lib/liftoff/project_configuration.rb b/lib/liftoff/project_configuration.rb index 77120de..4d42d16 100644 --- a/lib/liftoff/project_configuration.rb +++ b/lib/liftoff/project_configuration.rb @@ -6,6 +6,7 @@ class ProjectConfiguration :company, :prefix, :configure_git, + :enable_settings, :warnings_as_errors, :enable_static_analyzer, :indentation_level, diff --git a/lib/liftoff/settings_generator.rb b/lib/liftoff/settings_generator.rb new file mode 100644 index 0000000..cae56f7 --- /dev/null +++ b/lib/liftoff/settings_generator.rb @@ -0,0 +1,42 @@ +module Liftoff + class SettingsGenerator + def initialize(config) + @config = config + end + + def generate + if @config.enable_settings + move_settings_bundle + end + end + + def move_settings_bundle + FileManager.new.generate('Settings.bundle', 'Resources/Settings.bundle', @config) + parent_group = xcode_project['Resources'] || xcode_project.main_group + if (parent_group) + file = parent_group.new_file('Settings.bundle') + target.add_resources([file]) + xcode_project.save + end + end + + private + + def available_targets + xcode_project.targets.to_a.reject { |t| t.name.end_with?('Tests') } + end + + def target + @target ||= ObjectPicker.choose_item('target', available_targets) + end + + def xcode_project + @xcode_project ||= Xcodeproj::Project.open(project_file) + end + + def project_file + @project_file ||= ObjectPicker.choose_item('project', Dir.glob('*.xcodeproj')) + end + + end +end diff --git a/lib/liftoff/string_renderer.rb b/lib/liftoff/string_renderer.rb index 3853ac3..e9413e9 100644 --- a/lib/liftoff/string_renderer.rb +++ b/lib/liftoff/string_renderer.rb @@ -1,11 +1,11 @@ module Liftoff class StringRenderer - def initialize(configuration) - @configuration = configuration + def initialize(config) + @config = config end def render(string) - ERB.new(string).result(@configuration.get_binding) + ERB.new(string).result(@config.get_binding) end end end diff --git a/templates/Podfile b/templates/Podfile index c0d1423..9e544ff 100644 --- a/templates/Podfile +++ b/templates/Podfile @@ -11,3 +11,19 @@ target :unit_tests, :exclusive => true do pod 'OCMock' pod 'OHHTTPStubs' end + +<% if enable_settings && use_cocoapods %> +# Copy acknowledgements to the Settings.bundle + +post_install do | installer | + require 'fileutils' + + pods_acknowledgements_path = 'Pods/Target Support Files/Pods/Pods-Acknowledgements.plist' + settings_bundle_path = Dir.glob("**/*Settings.bundle*").first + + if File.file?(pods_acknowledgements_path) + puts 'Copying acknowledgements to Settings.bundle' + FileUtils.cp_r(pods_acknowledgements_path, "#{settings_bundle_path}/Acknowledgements.plist", :remove_destination => true) + end +end +<% end %> diff --git a/templates/Settings.bundle/Root.plist b/templates/Settings.bundle/Root.plist new file mode 100644 index 0000000..fd17a0b --- /dev/null +++ b/templates/Settings.bundle/Root.plist @@ -0,0 +1,25 @@ + + + + + PreferenceSpecifiers + +<% if use_cocoapods %> + + Type + PSGroupSpecifier + + + File + Acknowledgements + Title + Acknowledgements + Type + PSChildPaneSpecifier + +<% end %> + + StringsTable + Root + + diff --git a/templates/Settings.bundle/en.lproj/Root.strings b/templates/Settings.bundle/en.lproj/Root.strings new file mode 100644 index 0000000..b4ff77d --- /dev/null +++ b/templates/Settings.bundle/en.lproj/Root.strings @@ -0,0 +1,3 @@ +/* A single strings file, whose title is specified in your preferences schema. The strings files provide the localized content to display to the user for each of your preferences. */ + +"Acknowledgements" = "Acknowledgements";