Skip to content

Commit

Permalink
Add Settings bundle
Browse files Browse the repository at this point in the history
This adds the ability to include a settings bundle in your app. The
bundle is added by default, and includes CocoaPods attributions if
CocoaPods is enabled.
  • Loading branch information
bitcrumb authored and gfontenot committed Nov 19, 2014
1 parent 751a08e commit a2d9d90
Show file tree
Hide file tree
Showing 12 changed files with 122 additions and 13 deletions.
1 change: 1 addition & 0 deletions defaults/liftoffrc
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ enable_static_analyzer: true
indentation_level: 4
use_tabs: false
use_cocoapods: true
enable_settings: true
strict_prompts: false

run_script_phases:
Expand Down
2 changes: 2 additions & 0 deletions lib/liftoff.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
4 changes: 4 additions & 0 deletions lib/liftoff/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
28 changes: 19 additions & 9 deletions lib/liftoff/file_manager.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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)
Expand All @@ -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|
Expand Down
5 changes: 5 additions & 0 deletions lib/liftoff/launchpad.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ def liftoff(options)
generate_project
setup_cocoapods
generate_templates
generate_settings
install_cocoapods
perform_project_actions
open_project
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/liftoff/project_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions lib/liftoff/project_configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ class ProjectConfiguration
:prefix,
:test_target_name,
:configure_git,
:enable_settings,
:warnings_as_errors,
:enable_static_analyzer,
:indentation_level,
Expand Down
42 changes: 42 additions & 0 deletions lib/liftoff/settings_generator.rb
Original file line number Diff line number Diff line change
@@ -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
6 changes: 3 additions & 3 deletions lib/liftoff/string_renderer.rb
Original file line number Diff line number Diff line change
@@ -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
16 changes: 16 additions & 0 deletions templates/Podfile
Original file line number Diff line number Diff line change
Expand Up @@ -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 %>
25 changes: 25 additions & 0 deletions templates/Settings.bundle/Root.plist
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>PreferenceSpecifiers</key>
<array>
<% if use_cocoapods %>
<dict>
<key>Type</key>
<string>PSGroupSpecifier</string>
</dict>
<dict>
<key>File</key>
<string>Acknowledgements</string>
<key>Title</key>
<string>Acknowledgements</string>
<key>Type</key>
<string>PSChildPaneSpecifier</string>
</dict>
<% end %>
</array>
<key>StringsTable</key>
<string>Root</string>
</dict>
</plist>
3 changes: 3 additions & 0 deletions templates/Settings.bundle/en.lproj/Root.strings
Original file line number Diff line number Diff line change
@@ -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";

0 comments on commit a2d9d90

Please sign in to comment.