require 'colored' require 'fileutils' require 'psych' require 'yaml' require 'thor' require 'yk_command/project/yk_project' require_relative 'module_handler' module YkCommand MODULE_CONFIG_FILE = '.YKModuleFilesConfig.yml'.freeze class YkModule < Thor include Thor::Actions no_commands do def initialize(args = [], options = {}, config = {}) super end def generate(path = nil, name, lang, class_prefix, author) @name = name @module = @name @lang = lang @class_prefix = class_prefix @final_path = "#{path}/#{@name}" @author = author @prefixed_module = @class_prefix + @module say "generating file in path:#{@final_path}", :green if File.exist?(@final_path.to_s) say "#{@final_path} 已存在:", :red else construct_project path, false end end def read_user_input(desc, ask_prom, now_value, limit) item = '' say "#{desc}:", :green input = ask("#{ask_prom} [#{now_value}] ?") if input != '' item = input else if now_value != nil && now_value != '' item = now_value else say "请正确填写#{desc}", :red item = read_user_input(desc, ask_prom, now_value, limit) end end item end def create(path = nil, simple) path = Dir.pwd if path.nil? config_file_path = "#{path}/#{MODULE_CONFIG_FILE}" config = File.exist?(config_file_path) ? YAML.load_file(config_file_path) : {} project = read_user_input('模块名', "Project name", config[:project], nil) config[:project] = project language = read_user_input('语言', "Project language", config[:language], ['objc', 'swift', '']) config[:language] = language class_prefix = read_user_input('类名前缀', "Class prefix", config[:class_prefix], nil) config[:class_prefix] = class_prefix author = read_user_input('文件作者', "Author", config[:author], nil) config[:author] = author # Dir.chdir(path) File.open(config_file_path, 'w') do |f| f.write config.to_yaml end @name = config[:project] @class_prefix = config[:class_prefix] @prefixed_module = config[:class_prefix] + @name @project = config[:project] @author = config[:author] @date = Time.now.strftime('%d/%m/%y') @lang = config[:language] construct_project path, simple end def construct_project(path, is_simple) final_path = "#{path}/#{@name}" if File.exist?(final_path.to_s) say "#{final_path} 已存在:", :red else YkProject.new(is_simple).prepare_project(@name, @lang, @class_prefix, @author, path) config = {} config[:final_path] = final_path config[:name] = @name config[:project] = @project config[:prefixed_module] = @prefixed_module config[:author] = @author config[:date] = @date if !is_simple== true file_handler = ModuleHandler.new([], [], config) file_handler.yk_module_folders file_handler.yk_template_files end Dir.chdir("#{final_path}/Example") do system 'pod install' system "open './#{@name}.xcworkspace'" end end # # # # config = {} # if File.exist?(config_file_path) # # config = YAML.load_file(config_file_path) # name = read_user_input('模块名',"Project name",config) # # else # say '模块名:', :green # input_name = ask("Project name [#{config[:project]}] ?") # if input_name != '' # config[:project] = input_name if input_name != config[:project] # else # say '请正确填写模块名',:red # end # # say '语言:', :green # language = ask("Project language [#{config[:language]}] ?", limited_to: ['objc', 'swift', '']) # say '类名前缀:', :green # class_prefix = ask("Class prefix [#{config[:class_prefix]}] ?") # say '文件作者:', :green # author = ask("Author [#{config[:author]}] ?") # # config[:project] = project.empty? ? config[:project] || '' : project # config[:language] = language.empty? ? config[:language] || 'objc' : language # config[:class_prefix] = class_prefix.empty? ? config[:class_prefix] || '' : class_prefix # config[:author] = author.empty? ? config[:author] || '' : author # end # # # #读取模块创建的配置文件 # end # def read_config(path) # config_file_path = "#{path}/#{CONFIG_FILE}" # config = File.exist?(config_file_path) ? YAML.load_file(config_file_path) : {} # # project = @name # say '语言:', :green # language = ask("Project language [#{config[:language]}] ?", limited_to: ['objc', 'swift', '']) # say '类名前缀:', :green # class_prefix = ask("Class prefix [#{config[:class_prefix]}] ?") # say '文件作者:', :green # author = ask("Author [#{config[:author]}] ?") # # config[:project] = project.empty? ? config[:project] || '' : project # config[:language] = language.empty? ? config[:language] || 'objc' : language # config[:class_prefix] = class_prefix.empty? ? config[:class_prefix] || '' : class_prefix # config[:author] = author.empty? ? config[:author] || '' : author # # File.open(config_file_path, 'w') do |f| # f.write config.to_yaml # # f.write YAML.to_yaml(config) # end # # @module = @name # @class_prefix = config[:class_prefix] # @prefixed_module = config[:class_prefix] + @module # @project = config[:project] # @author = config[:author] # @date = Time.now.strftime('%d/%m/%y') # @lang = config[:language] # end # def prepare_folder # host_a = 'yeah' # host_b = 'ka' # template_repo_url = "http://gitlab.#{host_a}#{host_b}.com/App/iOS/YKComponents/YKProjectTemplate.git" # system("git clone #{template_repo_url} #{@final_path}") # FileUtils.remove_dir(@final_path, true) # FileUtils.cp_r('/Users/imacn24/Documents/dev/YKProjectTemplate', @final_path) # FileUtils.remove_dir("#{@final_path}/.git", true) # end end end end