diff --git a/.gitignore b/.gitignore index 4a8aa26..d71fa9c 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,5 @@ .idea/modules.xml .idea/vcs.xml .idea/workspace.xml +.rakeTasks +encodings.xml diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..4a96ab5 --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,137 @@ +# Change Log + +## [0.7.4](https://github.com/rambler-ios/Generamba/tree/0.7.4) (2016-02-21) +[Full Changelog](https://github.com/rambler-ios/Generamba/compare/0.7.3...0.7.4) + +**Implemented enhancements:** + +- Asks the user if he needed tests in `generamba setup` command [\#85](https://github.com/rambler-ios/Generamba/issues/85) +- Can we introduce something similar to view partials in rails \(for duplicated parts of views\) [\#62](https://github.com/rambler-ios/Generamba/issues/62) + +**Fixed bugs:** + +- Add resources\(xib, storyboard and etc\) in project bundle resources. [\#84](https://github.com/rambler-ios/Generamba/issues/84) + +**Closed issues:** + +- Get "Liquid error: This liquid context does not allow includes." on all files in just generated module [\#93](https://github.com/rambler-ios/Generamba/issues/93) + +## [0.7.3](https://github.com/rambler-ios/Generamba/tree/0.7.3) (2016-01-24) +[Full Changelog](https://github.com/rambler-ios/Generamba/compare/0.7.2...0.7.3) + +**Implemented enhancements:** + +- Enhance `generamba template search` to search templates in custom catalogs [\#75](https://github.com/rambler-ios/Generamba/issues/75) +- Enhance `generamba template list` to browse custom catalogs [\#74](https://github.com/rambler-ios/Generamba/issues/74) +- Warn the user if template contains missing dependencies [\#17](https://github.com/rambler-ios/Generamba/issues/17) + +**Closed issues:** + +- Can we use constants from rambafile in templates? [\#81](https://github.com/rambler-ios/Generamba/issues/81) +- Arguments for `generamba gen Name modulename` [\#80](https://github.com/rambler-ios/Generamba/issues/80) + +## [0.7.2](https://github.com/rambler-ios/Generamba/tree/0.7.2) (2016-01-10) +[Full Changelog](https://github.com/rambler-ios/Generamba/compare/0.7.1...0.7.2) + +**Implemented enhancements:** + +- --version command [\#54](https://github.com/rambler-ios/Generamba/issues/54) +- Incorrect behavior when generate module which already exists [\#52](https://github.com/rambler-ios/Generamba/issues/52) +- Add the ability to specify custom template catalogs in Rambafile [\#44](https://github.com/rambler-ios/Generamba/issues/44) + +**Fixed bugs:** + +- Don't set target in swift projects [\#65](https://github.com/rambler-ios/Generamba/issues/65) +- Incorrect behavior when generate module which already exists [\#52](https://github.com/rambler-ios/Generamba/issues/52) + +**Closed issues:** + +- Can generamba be used to generate swift modules? [\#61](https://github.com/rambler-ios/Generamba/issues/61) +- Git 1.2.9.1 [\#57](https://github.com/rambler-ios/Generamba/issues/57) +- Clarify generamba parameters usage [\#51](https://github.com/rambler-ios/Generamba/issues/51) +- Add documentation for liquid templates [\#48](https://github.com/rambler-ios/Generamba/issues/48) +- The documentation is not translated into English [\#13](https://github.com/rambler-ios/Generamba/issues/13) + +## [0.7.1](https://github.com/rambler-ios/Generamba/tree/0.7.1) (2015-12-20) +[Full Changelog](https://github.com/rambler-ios/Generamba/compare/0.7.0...0.7.1) + +**Implemented enhancements:** + +- Podfile path setup during generamba setup needs clarification [\#50](https://github.com/rambler-ios/Generamba/issues/50) +- Remove the Settingslogic dependency [\#42](https://github.com/rambler-ios/Generamba/issues/42) + +**Fixed bugs:** + +- Unexpected behavior when use --module\_path [\#53](https://github.com/rambler-ios/Generamba/issues/53) + +**Closed issues:** + +- Add support for multiple targets during generamba setup [\#49](https://github.com/rambler-ios/Generamba/issues/49) + +## [0.7.0](https://github.com/rambler-ios/Generamba/tree/0.7.0) (2015-12-13) +[Full Changelog](https://github.com/rambler-ios/Generamba/compare/0.6.2...0.7.0) + +**Implemented enhancements:** + +- Add coloured output [\#28](https://github.com/rambler-ios/Generamba/issues/28) +- Add -filepath and -grouppath options for `generamba gen` command [\#27](https://github.com/rambler-ios/Generamba/issues/27) +- Add a `generamba template search` command [\#26](https://github.com/rambler-ios/Generamba/issues/26) +- Add a `generamba template list` command [\#25](https://github.com/rambler-ios/Generamba/issues/25) +- Add the if statements to the Rambafile.liquid [\#22](https://github.com/rambler-ios/Generamba/issues/22) +- Target membership for module sources [\#11](https://github.com/rambler-ios/Generamba/issues/11) + +**Fixed bugs:** + +- Non-informative error message when running `generamba template install` in the wrong directory [\#39](https://github.com/rambler-ios/Generamba/issues/39) +- undefined method `each' for nil:NilClass \(NoMethodError\) [\#37](https://github.com/rambler-ios/Generamba/issues/37) + +## [0.6.2](https://github.com/rambler-ios/Generamba/tree/0.6.2) (2015-11-25) +[Full Changelog](https://github.com/rambler-ios/Generamba/compare/0.6.1...0.6.2) + +**Fixed bugs:** + +- Can't install template from git repo [\#36](https://github.com/rambler-ios/Generamba/issues/36) + +**Closed issues:** + +- Need more information about dependencies [\#34](https://github.com/rambler-ios/Generamba/issues/34) + +## [0.6.1](https://github.com/rambler-ios/Generamba/tree/0.6.1) (2015-11-21) +[Full Changelog](https://github.com/rambler-ios/Generamba/compare/0.6.0...0.6.1) + +**Implemented enhancements:** + +- Add Rambafile validation [\#10](https://github.com/rambler-ios/Generamba/issues/10) + +**Fixed bugs:** + +- The author name should be configured without `generamba setup` [\#31](https://github.com/rambler-ios/Generamba/issues/31) +- Sometimes Generamba fills in the wrong test target in the Rambafile [\#30](https://github.com/rambler-ios/Generamba/issues/30) +- Seems that Generamba doesn't add project name to the headers [\#29](https://github.com/rambler-ios/Generamba/issues/29) + +**Closed issues:** + +- Add a documentation for a .rambaspec file and overall template structure [\#24](https://github.com/rambler-ios/Generamba/issues/24) +- Rambafile format is not documented [\#9](https://github.com/rambler-ios/Generamba/issues/9) + +## [0.6.0](https://github.com/rambler-ios/Generamba/tree/0.6.0) (2015-11-15) +[Full Changelog](https://github.com/rambler-ios/Generamba/compare/0.5.0...0.6.0) + +**Implemented enhancements:** + +- Add a podfile\_path/cartfile\_path fields to a Rambafile [\#19](https://github.com/rambler-ios/Generamba/issues/19) +- Add a dependencies field to the .rambaspec [\#18](https://github.com/rambler-ios/Generamba/issues/18) +- Move the hardcoded keys to the constants [\#15](https://github.com/rambler-ios/Generamba/issues/15) +- Implement automatic template creation [\#8](https://github.com/rambler-ios/Generamba/issues/8) +- Change some of the questions of `generamba setup` [\#7](https://github.com/rambler-ios/Generamba/issues/7) + +**Fixed bugs:** + +- Rambafile uses absolute filepath to xcodeproj file [\#21](https://github.com/rambler-ios/Generamba/issues/21) +- Duplicate file reference in project.pbxproj [\#20](https://github.com/rambler-ios/Generamba/issues/20) +- Move the rambler\_viper\_controller template to the generamba-catalog [\#16](https://github.com/rambler-ios/Generamba/issues/16) + +## [0.5.0](https://github.com/rambler-ios/Generamba/tree/0.5.0) (2015-11-01) + + +\* *This Change Log was automatically generated by [github_changelog_generator](https://github.com/skywinder/Github-Changelog-Generator)* \ No newline at end of file diff --git a/Gemfile.lock b/Gemfile.lock index fc3b546..6032c46 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,12 +1,12 @@ PATH remote: . specs: - generamba (0.7.2) + generamba (0.7.4) cocoapods-core (= 0.39.0) git (= 1.2.9.1) liquid (= 3.0.6) + terminal-table (= 1.4.5) thor (= 0.19.1) - tilt (= 2.0.1) xcodeproj (= 0.28.2) GEM @@ -47,9 +47,9 @@ GEM diff-lcs (>= 1.2.0, < 2.0) rspec-support (~> 3.3.0) rspec-support (3.3.0) + terminal-table (1.4.5) thor (0.19.1) thread_safe (0.3.5) - tilt (2.0.1) tzinfo (1.2.2) thread_safe (~> 0.1) xcodeproj (0.28.2) diff --git a/README.md b/README.md index d827981..59bbbfe 100644 --- a/README.md +++ b/README.md @@ -5,6 +5,8 @@ **Generamba** is a code generator made for working with Xcode. Primarily it is designed to generate VIPER modules but it is quite easy to customize it for generation of any other classes (both in Objective-C and Swift). +*We keep evolving Generamba:* [changelog](https://github.com/rambler-ios/Generamba/blob/develop/CHANGELOG.md), [release notes](https://github.com/rambler-ios/Generamba/releases). + ![Generamba Screenshot](http://s24.postimg.org/gej9cg1cl/generamba.jpg) ### Key features @@ -24,7 +26,7 @@ When necessary you can install the required Ruby version with the help of [`rvm` Run the command `gem install generamba`. -### Using +### Usage 1. Run [`generamba setup`](https://github.com/rambler-ios/Generamba/wiki/Available-Commands#basic-generamba-configuration) in the project root folder. This command helps to create [Rambafile](https://github.com/rambler-ios/Generamba/wiki/Rambafile-Structure) that define all configuration needed to generate code. You can modify this file directly in future. 2. Add all templates planned to use in the project to the generated [Rambafile](https://github.com/rambler-ios/Generamba/wiki/Rambafile-Structure). You can begin with one of the templates from our catalog: `{name: 'rviper_controller'}`. 3. Run [`generamba template install`](https://github.com/rambler-ios/Generamba/wiki/Available-Commands#template-installation). All the templates will be placed in the '/Templates' folder of your current project. @@ -41,6 +43,7 @@ Run `generamba help` to learn more about each of the Generamba features. **Other materials:** - [Russian] Rambler.iOS V: Generamba and Code Generation ([Slides](http://www.slideshare.net/Rambler-iOS/viper-56423582) | [Video](http://www.youtube.com/watch?v=NXNiN9FaUnY)) +- [Introduction to Generamba](http://etolstoy.com/2016/02/10/generamba/) ### Authors diff --git a/generamba.gemspec b/generamba.gemspec index c4a7487..490903d 100644 --- a/generamba.gemspec +++ b/generamba.gemspec @@ -6,7 +6,7 @@ require 'generamba/version' Gem::Specification.new do |spec| spec.name = 'generamba' spec.version = Generamba::VERSION - spec.authors = ['Egor Tolstoy', 'Andrey Zarembo'] + spec.authors = ['Egor Tolstoy', 'Andrey Zarembo', 'Beniamin Sarkisyan'] spec.email = 'e.tolstoy@rambler-co.ru' spec.summary = 'Advanced code generator for Xcode projects with a nice and flexible template system.' @@ -23,9 +23,9 @@ Gem::Specification.new do |spec| spec.add_runtime_dependency 'thor', '0.19.1' spec.add_runtime_dependency 'xcodeproj', '0.28.2' spec.add_runtime_dependency 'liquid', '3.0.6' - spec.add_runtime_dependency 'tilt', '2.0.1' spec.add_runtime_dependency 'git', '1.2.9.1' spec.add_runtime_dependency 'cocoapods-core', '0.39.0' + spec.add_runtime_dependency 'terminal-table', '1.4.5' spec.add_development_dependency 'bundler', '~> 1.10' spec.add_development_dependency 'rake', '~> 10.0' diff --git a/lib/generamba/cli/cli.rb b/lib/generamba/cli/cli.rb index 1dba5cf..fbe68b0 100644 --- a/lib/generamba/cli/cli.rb +++ b/lib/generamba/cli/cli.rb @@ -1,7 +1,6 @@ require 'thor' require 'xcodeproj' require 'liquid' -require 'tilt' require 'git' require 'generamba/cli/gen_command.rb' require 'generamba/cli/setup_command.rb' diff --git a/lib/generamba/cli/gen_command.rb b/lib/generamba/cli/gen_command.rb index 5fbdef0..c5f1bef 100644 --- a/lib/generamba/cli/gen_command.rb +++ b/lib/generamba/cli/gen_command.rb @@ -1,7 +1,9 @@ require 'thor' +require 'generamba/helpers/print_table.rb' require 'generamba/helpers/rambafile_validator.rb' require 'generamba/helpers/xcodeproj_helper.rb' require 'generamba/helpers/dependency_checker.rb' +require 'generamba/helpers/gen_command_table_parameters_formatter.rb' module Generamba::CLI class Application < Thor @@ -38,6 +40,12 @@ def gen(module_name, template_name) rambafile = YAML.load_file(RAMBAFILE_NAME) + parameters = GenCommandTableParametersFormatter.prepare_parameters_for_displaying(rambafile) + PrintTable.print_values( + values: parameters, + title: "Summary for gen #{module_name}" + ) + template = ModuleTemplate.new(template_name) code_module = CodeModule.new(module_name, module_description, rambafile, options) diff --git a/lib/generamba/cli/setup_command.rb b/lib/generamba/cli/setup_command.rb index c498034..23ab4b5 100644 --- a/lib/generamba/cli/setup_command.rb +++ b/lib/generamba/cli/setup_command.rb @@ -1,9 +1,9 @@ require 'thor' require 'xcodeproj' require 'liquid' -require 'tilt' require 'git' require 'generamba/constants/rambafile_constants.rb' +require 'generamba/helpers/print_table.rb' module Generamba::CLI class Application < Thor @@ -33,21 +33,29 @@ def setup targets_prompt = '' project.targets.each_with_index { |element, i| targets_prompt += ("#{i}. #{element.name}" + "\n") } project_target = ask_index("Select the appropriate target for adding your MODULES (type the index):\n" + targets_prompt,project.targets) - test_target = ask_index("Select the appropriate target for adding your TESTS (type the index):\n" + targets_prompt,project.targets) + include_tests = yes?('Are you using unit-tests in this project? (yes/no)') + + if include_tests + test_target = ask_index("Select the appropriate target for adding your TESTS (type the index):\n" + targets_prompt,project.targets) + end should_use_same_paths = yes?('Do you want to use the same paths for your files both in Xcode and the filesystem? (yes/no)') if should_use_same_paths project_group_path = ask('The default path for creating new modules:') project_file_path = project_group_path - test_group_path = ask('The default path for creating tests:') - test_file_path = test_group_path + if include_tests + test_group_path = ask('The default path for creating tests:') + test_file_path = test_group_path + end else project_group_path = ask('The default path for creating new modules (in Xcode groups):') project_file_path = ask('The default path for creating new modules (in the filesystem):') - test_group_path = ask('The default path for creating tests (in Xcode groups):') - test_file_path = ask('The default path for creating tests (in the filesystem):') + if include_tests + test_group_path = ask('The default path for creating tests (in Xcode groups):') + test_file_path = ask('The default path for creating tests (in the filesystem):') + end end using_pods = yes?('Are you using Cocoapods? (yes/no)') @@ -65,9 +73,23 @@ def setup properties[PROJECT_TARGET_KEY] = project_target.name properties[PROJECT_FILE_PATH_KEY] = project_file_path properties[PROJECT_GROUP_PATH_KEY] = project_group_path - properties[TEST_TARGET_KEY] = test_target.name - properties[TEST_FILE_PATH_KEY] = test_file_path - properties[TEST_GROUP_PATH_KEY] = test_group_path + + if test_target + properties[TEST_TARGET_KEY] = test_target.name + end + + if test_file_path + properties[TEST_FILE_PATH_KEY] = test_file_path + end + + if test_group_path + properties[TEST_GROUP_PATH_KEY] = test_group_path + end + + PrintTable.print_values( + values: properties, + title: "Summary for generamba setup" + ) Generamba::RambafileGenerator.create_rambafile(properties) puts('Rambafile successfully created! Now add some templates to the Rambafile and run `generamba template install`.'.green) diff --git a/lib/generamba/cli/template/template_create_command.rb b/lib/generamba/cli/template/template_create_command.rb index a1aecbd..b5a77a1 100644 --- a/lib/generamba/cli/template/template_create_command.rb +++ b/lib/generamba/cli/template/template_create_command.rb @@ -1,3 +1,5 @@ +require 'generamba/helpers/print_table.rb' + module Generamba::CLI class Template < Thor include Generamba @@ -17,13 +19,22 @@ def create(template_name) TEMPLATE_NAME_KEY => template_name, TEMPLATE_SUMMARY_KEY => summary, TEMPLATE_AUTHOR_KEY => author, - TEMPLATE_LICENSE_KEY => license, - TEMPLATE_DEPENDENCIES_KEY => dependencies + TEMPLATE_LICENSE_KEY => license } + if dependencies and !dependencies.empty? + properties[TEMPLATE_DEPENDENCIES_KEY] = dependencies + end + + PrintTable.print_values( + values: properties, + title: "Summary for template create" + ) + template_creator = Generamba::TemplateCreator.new template_creator.create_template(properties) puts("The template #{template_name} is successfully generated! Now add some file templates into it.".green) end + end end \ No newline at end of file diff --git a/lib/generamba/code_generation/Rambafile.liquid b/lib/generamba/code_generation/Rambafile.liquid index 62767e0..b223e90 100644 --- a/lib/generamba/code_generation/Rambafile.liquid +++ b/lib/generamba/code_generation/Rambafile.liquid @@ -16,15 +16,16 @@ project_file_path: {{ project_file_path }} # The Xcode group path to new modules project_group_path: {{ project_group_path }} -{% if test_target != "" or test_file_path != "" or test_group_path != "" %}### Tests generation settings section{% endif %} -{% if test_target != "" %}# The tests target name +{% if (test_target != nil and test_target != "") or (test_file_path != nil and test_file_path != "") or (test_group_path != nil and test_group_path != "") %}### Tests generation settings section +{% if test_target != nil and test_target != "" %}# The tests target name test_target: {{ test_target }}{% endif %} -{% if test_file_path != "" %}# The file path for new tests +{% if test_file_path != nil and test_file_path != "" %}# The file path for new tests test_file_path: {{ test_file_path }}{% endif %} -{% if test_group_path != "" %}# The Xcode group path to new tests +{% if test_group_path != nil and test_group_path != "" %}# The Xcode group path to new tests test_group_path: {{ test_group_path }}{% endif %} +{% endif %} {% if podfile_path != nil or cartfile_path != nil %}### Dependencies settings section{% endif %} {% if podfile_path != nil %}podfile_path: {{ podfile_path }}{% endif %} diff --git a/lib/generamba/code_generation/content_generator.rb b/lib/generamba/code_generation/content_generator.rb index 5088109..602e966 100755 --- a/lib/generamba/code_generation/content_generator.rb +++ b/lib/generamba/code_generation/content_generator.rb @@ -1,5 +1,4 @@ require 'liquid' -require 'tilt' module Generamba @@ -13,13 +12,17 @@ class ContentGenerator # # @return [String] The generated body def self.create_file_content(file, code_module, template) - file_template = Tilt.new(template.template_path.join(file[TEMPLATE_FILE_PATH_KEY])) - file_name = File.basename(file[TEMPLATE_FILE_NAME_KEY]) + file_source = IO.read(template.template_path.join(file[TEMPLATE_FILE_PATH_KEY])) + Liquid::Template.file_system = Liquid::LocalFileSystem.new(template.template_path.join('snippets'), '%s.liquid') + + template = Liquid::Template.parse(file_source) + file_name = File.basename(file[TEMPLATE_FILE_NAME_KEY]) + module_info = { 'name' => code_module.name, 'file_name' => file_name, 'description' => code_module.description, - 'project_name' => code_module.project_name + 'project_name' => code_module.project_name } developer = { @@ -35,7 +38,8 @@ def self.create_file_content(file, code_module, template) 'prefix' => code_module.prefix } - output = file_template.render(scope) + output = template.render(scope) + return output end end diff --git a/lib/generamba/code_generation/rambafile_generator.rb b/lib/generamba/code_generation/rambafile_generator.rb index 957871f..9b942c7 100644 --- a/lib/generamba/code_generation/rambafile_generator.rb +++ b/lib/generamba/code_generation/rambafile_generator.rb @@ -1,5 +1,4 @@ require 'liquid' -require 'tilt' module Generamba @@ -11,8 +10,10 @@ class RambafileGenerator # # @return void def self.create_rambafile(properties) - template = Tilt.new(File.dirname(__FILE__) + '/Rambafile.liquid') - output = template.render(properties) + file_source = IO.read(File.dirname(__FILE__) + '/Rambafile.liquid') + + template = Liquid::Template.parse(file_source) + output = template.render(properties).gsub!(/[\n]{3,}/, "\n\n"); File.open(RAMBAFILE_NAME, 'w+') {|f| f.write(output) diff --git a/lib/generamba/constants/rambafile_constants.rb b/lib/generamba/constants/rambafile_constants.rb index ef35a5f..7fb6533 100644 --- a/lib/generamba/constants/rambafile_constants.rb +++ b/lib/generamba/constants/rambafile_constants.rb @@ -24,4 +24,5 @@ module Generamba TEMPLATE_DECLARATION_NAME_KEY = 'name' TEMPLATE_DECLARATION_LOCAL_KEY = 'local' TEMPLATE_DECLARATION_GIT_KEY = 'git' + TEMPLATE_DECLARATION_BRANCH_KEY = 'branch' end \ No newline at end of file diff --git a/lib/generamba/helpers/gen_command_table_parameters_formatter.rb b/lib/generamba/helpers/gen_command_table_parameters_formatter.rb new file mode 100644 index 0000000..c39846d --- /dev/null +++ b/lib/generamba/helpers/gen_command_table_parameters_formatter.rb @@ -0,0 +1,23 @@ +module Generamba + + # Provides methods for prepare parameters for displaying in table. + class GenCommandTableParametersFormatter + + # This method prepared parameter for displaying + def self.prepare_parameters_for_displaying(parameters) + params = parameters.clone + + templates = [] + + params['templates'].each do |param| + templates.push(param['name']) + end + + params['templates'] = templates.join("\n") + + return params + end + + end + +end diff --git a/lib/generamba/helpers/print_table.rb b/lib/generamba/helpers/print_table.rb new file mode 100644 index 0000000..aa69837 --- /dev/null +++ b/lib/generamba/helpers/print_table.rb @@ -0,0 +1,21 @@ +module Generamba + + # Provides methods for print parameters in nice table. + class PrintTable + + # This method prints out all the user inputs in a nice table. + def self.print_values(values: nil, title: nil) + require 'terminal-table' + + params = {} + params[:rows] = values + params[:title] = title.green if title + + puts '' + puts Terminal::Table.new(params) + puts '' + end + + end + +end \ No newline at end of file diff --git a/lib/generamba/helpers/xcodeproj_helper.rb b/lib/generamba/helpers/xcodeproj_helper.rb index d19e916..5d1e417 100644 --- a/lib/generamba/helpers/xcodeproj_helper.rb +++ b/lib/generamba/helpers/xcodeproj_helper.rb @@ -25,18 +25,32 @@ def self.add_file_to_project_and_targets(project, targets_name, group_path, file file_name = File.basename(file_path) targets_name.each do |target| xcode_target = self.obtain_target(target, project) - xcode_target.add_file_references([xcode_file]) - end if self.add_file_to_target?(file_name) + + if self.is_compile_source?(file_name) + xcode_target.add_file_references([xcode_file]) + elsif self.is_bundle_resource?(file_name) + xcode_target.add_resources([xcode_file]) + end + + end end - # Decides should file be added - # @param file_name [String] Array of targets name + # File is a compiled source + # @param file_name [String] String of file name # # @return [TrueClass or FalseClass] - def self.add_file_to_target?(file_name) + def self.is_compile_source?(file_name) File.extname(file_name) == '.m' || File.extname(file_name) == '.swift' || File.extname(file_name) == '.mm' end + # File is a resource + # @param resource_name [String] String of resource name + # + # @return [TrueClass or FalseClass] + def self.is_bundle_resource?(resource_name) + File.extname(resource_name) == '.xib' || File.extname(resource_name) == '.storyboard' + end + # Recursively clears children of the given group # @param project [Xcodeproj::Project] The working Xcode project file # @param group_path [Pathname] The full group path @@ -75,7 +89,7 @@ def self.module_with_group_path_already_exists(project, group_path) # # @return [PBXGroup] def self.retreive_group_or_create_if_needed(group_path, project, create_group_if_not_exists) - group_names = group_names_from_group_path(group_path) + group_names = path_names_from_path(group_path) final_group = project @@ -112,13 +126,13 @@ def self.obtain_target(target_name, project) raise StandardError.new(error_description) end - # Splits the provided Xcode group path to an array of separate groups - # @param group_path The full group path + # Splits the provided Xcode path to an array of separate paths + # @param path The full group or file path # # @return [[String]] - def self.group_names_from_group_path(group_path) - groups = group_path.to_s.split('/') - return groups + def self.path_names_from_path(path) + paths = path.to_s.split('/') + return paths end # Remove build file from target build phase @@ -128,14 +142,28 @@ def self.group_names_from_group_path(group_path) # # @return [Void] def self.remove_file_by_file_path(file_path, targets_name, project) - build_phases = self.build_phases_from_targets(targets_name, project) - + file_names = path_names_from_path(file_path) + + build_phases = nil + + if self.is_compile_source?(file_names.last) + build_phases = self.build_phases_from_targets(targets_name, project) + elsif self.is_bundle_resource?(file_names.last) + build_phases = self.resources_build_phase_from_targets(targets_name, project) + end + + self.remove_file_from_build_phases(file_path, build_phases) + end + + def self.remove_file_from_build_phases(file_path, build_phases) + return if build_phases == nil + build_phases.each do |build_phase| build_phase.files.each do |build_file| next if build_file.nil? || build_file.file_ref.nil? build_file_path = self.configure_file_ref_path(build_file.file_ref) - + if build_file_path == file_path build_phase.remove_build_file(build_file) end @@ -163,6 +191,22 @@ def self.build_phases_from_targets(targets_name, project) return build_phases end + # Find and return target resources build phase + # @param targets_name [String] Array of targets + # @param project [Xcodeproj::Project] The target xcodeproj file + # + # @return [[PBXResourcesBuildPhase]] + def self.resources_build_phase_from_targets(targets_name, project) + resource_build_phase = [] + + targets_name.each do |target_name| + xcode_target = self.obtain_target(target_name, project) + resource_build_phase.push(xcode_target.resources_build_phase) + end + + return resource_build_phase + end + # Get configure file full path # @param file_ref [PBXFileReference] Build file # diff --git a/lib/generamba/template/creator/template_creator.rb b/lib/generamba/template/creator/template_creator.rb index 692aea6..57be442 100644 --- a/lib/generamba/template/creator/template_creator.rb +++ b/lib/generamba/template/creator/template_creator.rb @@ -13,13 +13,14 @@ class TemplateCreator # # @return [Void] def create_template(properties) - template_dir_path = Pathname.new(File.dirname(__FILE__)) - .join(NEW_TEMPLATE_FOLDER) + template_dir_path = Pathname.new(File.dirname(__FILE__)).join(NEW_TEMPLATE_FOLDER) rambaspec_template_file_path = template_dir_path.join(RAMBASPEC_TEMPLATE_NAME) code_file_path = template_dir_path.join(CODE_FOLDER) tests_file_path = template_dir_path.join(TESTS_FOLDER) - template = Tilt.new(rambaspec_template_file_path) + file_source = IO.read(rambaspec_template_file_path) + + template = Liquid::Template.parse(file_source) output = template.render(properties) result_name = properties[TEMPLATE_NAME_KEY] + RAMBASPEC_EXTENSION diff --git a/lib/generamba/template/helpers/rambaspec_validator.rb b/lib/generamba/template/helpers/rambaspec_validator.rb index 4f130a7..a52c92b 100644 --- a/lib/generamba/template/helpers/rambaspec_validator.rb +++ b/lib/generamba/template/helpers/rambaspec_validator.rb @@ -42,8 +42,7 @@ def self.validate_spec(template_name, template_path) # @return [Bool] def self.obtain_spec_path(template_name, template_path) spec_filename = template_name + RAMBASPEC_EXTENSION - Pathname.new(template_path) - .join(spec_filename) + Pathname.new(template_path).join(spec_filename) end end end \ No newline at end of file diff --git a/lib/generamba/template/installer/remote_installer.rb b/lib/generamba/template/installer/remote_installer.rb index 42266fa..d369acc 100644 --- a/lib/generamba/template/installer/remote_installer.rb +++ b/lib/generamba/template/installer/remote_installer.rb @@ -13,20 +13,27 @@ def install_template(template_declaration) puts("Installing #{template_name}...") repo_url = template_declaration.git + repo_branch = template_declaration.branch temp_path = Dir.tmpdir() - template_dir = Pathname.new(temp_path) - .join(template_name) - Git.clone(repo_url, template_name, :path => temp_path) + template_dir = Pathname.new(temp_path).join(template_name) + + if repo_branch != nil + Git.export(repo_url, template_name, :branch => repo_branch, :path => temp_path) + else + Git.clone(repo_url, template_name, :path => temp_path) + end + + template_path = "#{template_dir}/#{template_name}" - rambaspec_exist = Generamba::RambaspecValidator.validate_spec_existance(template_name, template_dir) + rambaspec_exist = Generamba::RambaspecValidator.validate_spec_existance(template_name, template_path) unless rambaspec_exist FileUtils.rm_rf(temp_path) error_description = "Cannot find #{template_name + RAMBASPEC_EXTENSION} in the root directory of specified repository.".red raise StandardError.new(error_description) end - rambaspec_valid = Generamba::RambaspecValidator.validate_spec(template_name, template_dir) + rambaspec_valid = Generamba::RambaspecValidator.validate_spec(template_name, template_path) unless rambaspec_valid error_description = "#{template_name + RAMBASPEC_EXTENSION} is not valid.".red raise StandardError.new(error_description) @@ -35,7 +42,7 @@ def install_template(template_declaration) install_path = Pathname.new(TEMPLATES_FOLDER) .join(template_name) FileUtils.mkdir_p install_path - FileUtils.copy_entry(template_dir, install_path) + FileUtils.copy_entry(template_path, install_path) FileUtils.rm_rf(temp_path) end diff --git a/lib/generamba/template/processor/template_declaration.rb b/lib/generamba/template/processor/template_declaration.rb index 9aba15c..5640e2b 100644 --- a/lib/generamba/template/processor/template_declaration.rb +++ b/lib/generamba/template/processor/template_declaration.rb @@ -15,12 +15,13 @@ class TemplateDeclarationType # Describes a Generamba template declaration model class TemplateDeclaration - attr_reader :name, :local, :git, :type + attr_reader :name, :local, :git, :branch, :type def initialize(template_hash) @name = template_hash[TEMPLATE_DECLARATION_NAME_KEY] @local = template_hash[TEMPLATE_DECLARATION_LOCAL_KEY] @git = template_hash[TEMPLATE_DECLARATION_GIT_KEY] + @branch = template_hash[TEMPLATE_DECLARATION_BRANCH_KEY] @type = TemplateDeclarationType::LOCAL_TEMPLATE if @local @type = TemplateDeclarationType::REMOTE_TEMPLATE if @git diff --git a/lib/generamba/version.rb b/lib/generamba/version.rb index ec82b3a..d40f1ec 100644 --- a/lib/generamba/version.rb +++ b/lib/generamba/version.rb @@ -1,3 +1,3 @@ module Generamba - VERSION = '0.7.3' + VERSION = '0.7.5' end diff --git a/spec/gen_command_table_parameters_formatter_spec.rb b/spec/gen_command_table_parameters_formatter_spec.rb new file mode 100644 index 0000000..d4da0e3 --- /dev/null +++ b/spec/gen_command_table_parameters_formatter_spec.rb @@ -0,0 +1,22 @@ +require_relative 'spec_helper' + +describe 'GenCommandTableParametersFormatter' do + + describe 'method prepare_parameters_for_displaying' do + it 'should convert array of hash to array of strings' do + expected_string = "name1\nname2" + + parameters = { + 'templates' => [ + {'name' => 'name1', 'path' => 'path1'}, + {'name' => 'name2', 'path' => 'path2'} + ] + } + + params = Generamba::GenCommandTableParametersFormatter.prepare_parameters_for_displaying(parameters) + + expect(params['templates']).to eq(expected_string) + end + end + +end diff --git a/spec/xcodeproj_helper_spec.rb b/spec/xcodeproj_helper_spec.rb index 7c12c50..0d47b90 100644 --- a/spec/xcodeproj_helper_spec.rb +++ b/spec/xcodeproj_helper_spec.rb @@ -4,19 +4,33 @@ describe 'method add_file_to_target?' do it 'should return true for Swift file' do filename = 'Test.swift' - result = Generamba::XcodeprojHelper::add_file_to_target?(filename) + result = Generamba::XcodeprojHelper::is_compile_source?(filename) expect(result).to eq(true) end it 'should return true for Obj-C file' do filename = 'Test.m' - result = Generamba::XcodeprojHelper::add_file_to_target?(filename) + result = Generamba::XcodeprojHelper::is_compile_source?(filename) expect(result).to eq(true) end it 'should return true for C++ file' do filename = 'Test.mm' - result = Generamba::XcodeprojHelper::add_file_to_target?(filename) + result = Generamba::XcodeprojHelper::is_compile_source?(filename) + expect(result).to eq(true) + end + end + + describe 'method add_file_to_bundle_resources?' do + it 'should return true for Xib file' do + resource_name = 'Test.xib' + result = Generamba::XcodeprojHelper::is_bundle_resource?(resource_name) + expect(result).to eq(true) + end + + it 'should return true for Storyboard file' do + resource_name = 'Test.storyboard' + result = Generamba::XcodeprojHelper::is_bundle_resource?(resource_name) expect(result).to eq(true) end end