Class: Commands

Inherits:
Thor
  • Object
show all
Includes:
Thor::Actions
Defined in:
lib/YKModuleCommand.rb

Constant Summary collapse

CONFIG_FILE =
'.YKModuleFilesConfig.yml'

Instance Method Summary collapse

Instance Method Details

#create(path = nil) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/YKModuleCommand.rb', line 49

def create(path=nil)

  if path == nil
    path = __dir__
  end

  say "模块名:",:green

  config_file_path = "#{path}/#{CONFIG_FILE}"

  config = File.exists?(config_file_path) ? YAML.load_file(config_file_path) : {}

  input_name      = ask("Project name [#{config[:project]}] ?")
  if input_name != ""
    @name = input_name
    if input_name != config[:project]
      config[:project] = input_name
    end
  else
    @name = config[:project]
  end

  File.open(config_file_path, 'w') do |f|
    f.write config.to_yaml
  end

  @final_path = "#{path}/#{@name}"

  if File.exist?("#{@final_path}")
    say "#{@final_path} 已存在:",:red
  else
    prepare_folder
    read_config(path)

    if File.exist?("#{@final_path}/configure")
      system("#{@final_path}/configure", @name, @lang ,@class_prefix ,  *@additional_args)
    else
      say "Template does not have a configure file", :red
    end

    yk_module_folders
    yk_template_files

  end



end

#prepare_folderObject



98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/YKModuleCommand.rb', line 98

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

#read_config(path) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/YKModuleCommand.rb', line 13

def read_config(path)
  config_file_path = "#{path}/#{CONFIG_FILE}"
  config = File.exists?(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]
  @prefixed_module = "YK#{@name}"

end

#yk_module_foldersObject



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/YKModuleCommand.rb', line 111

def yk_module_folders

  class_folder_path = "#{@final_path}/#{@name}/Classes"

  first_level_folders = ["Public","Private"]
  public_level_folders = ["Register"]
  private_level_folders = ["Category","Vendor","Tools"]


  first_level_folders.each do |folder|
    path = "#{class_folder_path}/#{folder}"
    empty_directory path
  end

  public_level_folders.each  do |folder|
    path = "#{class_folder_path}/Public/#{folder}"
    empty_directory path
  end

  private_level_folders.each  do |folder|
    path = "#{class_folder_path}/Private/#{folder}"
    empty_directory path
    system "touch \"#{path}/EmptyFile.m\""
  end

end

#yk_template_filesObject



140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/YKModuleCommand.rb', line 140

def yk_template_files

  register_path = "#{@final_path}/#{@name}/Classes/Public/Register"
  registger = {
    'RouterRegister.h'     => 'RouterRegister',
    'RouterRegister.m'     => 'RouterRegister',
    'ServiceRegister.h'     => 'ServiceRegister',
    'ServiceRegister.m'     => 'ServiceRegister',
  }


  registger.each do |file_name, folder|
    final_file =  "#{register_path}/#{@prefixed_module}#{file_name}"
    template "#{__dir__}/YKModuleCommand/template/objc/#{file_name}",final_file
  end

  Dir.chdir("#{@final_path}/Example") do
    system "pod install --silent"
  end

end