Class: TYCICore::ODMbuild

Inherits:
Object
  • Object
show all
Defined in:
lib/tuya/ci/core/odm_build.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(template_config, default_config = nil) ⇒ ODMbuild

Returns a new instance of ODMbuild.



10
11
12
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/tuya/ci/core/odm_build.rb', line 10

def initialize (template_config, default_config=nil)

    if validate(template_config)   # 数据校验通过以后,再进行赋值
    @template_config = template_config  # 客户选择的模板的配置信息

    puts "ODM Builder template is : \n#{@template_config}"

    @group_name = @template_config["git"]["group"]
    puts "ODM Builder group name is : #{@group_name}"

    @group_spec = "#{@group_name}Specs"
    group_host= @template_config["git"]["host"]
    @group_spec_url = "#{group_host}/#{@group_name}/#{@group_spec}.git"
    puts "ODM Builder group spec #{@group_spec} #{@group_spec_url}"

    @base_branch = @template_config["baseBranch"]
    puts "ODM Builder base branch is : #{@base_branch}"

    @build_branch = @template_config["branchName"]
    puts "ODM Builder target branch is : #{@build_branch}"

    @default_config = default_config unless default_config.nil? || default_config.empty?    # 默认模板的部分配置信息
    if @default_config.nil? || @default_config.empty?
      # 业务类型 1-登录,2-首页,3-场景,4-我的,5-application 100-其他
      @default_config = {
        "1" => {
            "module" => "TYLoginModule2"
        },
        "2" => {
            "module" => "TYSmartHouse",
            "skeleton" => "TYSmartHouseUISkeleton",
            "biz" => "TYSmartHouseBizKit"
        },
        "3" => {
            "module" => "TYSmartSceneModule",
            "biz" => "TYSmartSceneBizKit"
        },
        "4" => {
            "module" => "TYUserCenterModule",
            "biz" => "TYUserCenterBizKit"
        },
        "5" => {
            "module" => "TYSmartApplication"
        }
      }
    end

    puts "odm builder initial finish".green
  end
end

Instance Attribute Details

#base_branchObject

Returns the value of attribute base_branch.



9
10
11
# File 'lib/tuya/ci/core/odm_build.rb', line 9

def base_branch
  @base_branch
end

#build_branchObject

Returns the value of attribute build_branch.



9
10
11
# File 'lib/tuya/ci/core/odm_build.rb', line 9

def build_branch
  @build_branch
end

#group_nameObject

Returns the value of attribute group_name.



9
10
11
# File 'lib/tuya/ci/core/odm_build.rb', line 9

def group_name
  @group_name
end

#group_specObject

Returns the value of attribute group_spec.



9
10
11
# File 'lib/tuya/ci/core/odm_build.rb', line 9

def group_spec
  @group_spec
end

#template_configObject

Returns the value of attribute template_config.



9
10
11
# File 'lib/tuya/ci/core/odm_build.rb', line 9

def template_config
  @template_config
end

Instance Method Details

#buildObject

开始预编译



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/tuya/ci/core/odm_build.rb', line 78

def build   # 开始预编译
  raise "template config is invalid".red if @template_config.nil? || @template_config.empty?

  # 检查、创建分支
  check_branch()

  # 根据备份 重置podfile
  reset_podfile()

  # 根据配置 更新podfile 更新modules.json
  update_podfile_and_moodule_config()

  # pod update
  pod_update()

  # 根据pods,生成config_modules.json中的modules部分

  puts "ODM Builder setup_modules in Pods"
  @module_config.setup_modules()

  # 更新config_module.json
  @module_config.save()


  # if @is_initial
    TYCiCore::Git.git_commit_modify("feat: ODM builder config update")
    TYCiCore::Git.git_push
  puts "ODM builder config commit finish".green
  # end
end

#check_branchObject

def test

reset_podfile()
update_podfile_and_moodule_config()
@module_config.save()

end



115
116
117
118
119
120
121
# File 'lib/tuya/ci/core/odm_build.rb', line 115

def check_branch
  # 切换/创建 分支
  TYCiCore::Git.git_checkout_branch(@base_branch) unless TYCiCore::Git.git_current_branch == @base_branch || TYCiCore::Git.git_current_branch == @build_branch
  TYCiCore::Git.git_checkout_branch(@build_branch) unless TYCiCore::Git.git_current_branch == @build_branch

  puts "ODM builder checkout branch finish".green
end

#default_biz_kit_name(type) ⇒ Object

根据所给type,返回默认模块的biz名



242
243
244
# File 'lib/tuya/ci/core/odm_build.rb', line 242

def default_biz_kit_name(type)  # 根据所给type,返回默认模块的biz名
  @default_config[type.to_s]["biz"] unless @default_config[type.to_s].nil? || @default_config[type.to_s].empty?
end

#default_module_name(type) ⇒ Object

根据所给type,返回默认模块名



236
237
238
# File 'lib/tuya/ci/core/odm_build.rb', line 236

def default_module_name(type) # 根据所给type,返回默认模块名
  @default_config[type.to_s]["module"] unless @default_config[type.to_s].nil? || @default_config[type.to_s].empty?
end

#default_skeleton_name(type) ⇒ Object

根据所给type,返回默认模块的骨架名



239
240
241
# File 'lib/tuya/ci/core/odm_build.rb', line 239

def default_skeleton_name(type) # 根据所给type,返回默认模块的骨架名
  @default_config[type.to_s]["skeleton"] unless @default_config[type.to_s].nil? || @default_config[type.to_s].empty?
end

#pod_updateObject



214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
# File 'lib/tuya/ci/core/odm_build.rb', line 214

def pod_update
  if @group_spec_url.nil? || @group_spec_url.empty?
    TYCiCore::EXE.exe('pod', 'update')
  else
    home = ENV["HOME"]
    repo_local = "#{home}/.cocoapods/repos/#{@group_spec}"
    # TYCiCore::EXE.exe('pod', %W'repo add #{@group_spec} #{@group_spec_url}') unless File.exist?repo_local
    if !File.exist?repo_local
      `pod repo add #{@group_spec} #{@group_spec_url}`
    end

    pod_commands = [
    %W(repo update #{@group_spec}),
    %W(update --no-repo-update)
    ]
    TYCiCore::EXE.multi_exe('pod', pod_commands, true)

    puts "ODM builder pod update finish".green
  end
end

#reset_podfileObject



123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/tuya/ci/core/odm_build.rb', line 123

def reset_podfile
  podfile_backup_path = "./.podfile_backup"
  podfile_path = "./Podfile"

  if File.exist? podfile_backup_path
    podfile = File.open(podfile_path, "w")
    podfile << File.read(podfile_backup_path)
    podfile.close
  else
    podfile_backup = File.new(podfile_backup_path, "w")
    podfile_backup << File.read(podfile_path)
    podfile_backup.close
  end
end

#update_podfile_and_moodule_configObject



138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
# File 'lib/tuya/ci/core/odm_build.rb', line 138

def update_podfile_and_moodule_config
  @module_config = TYCiCore::ODMConfigModules.new()

  @module_config.update_config("scheme", @group_name) unless @group_name.nil? || @group_name.empty?
  @module_config.content["tabs"].clear


  podfile = TYCiCore::Podfile.new()
  podfile.source_add(@group_spec_url) unless @group_spec_url.nil? || @group_spec_url.empty?

  @template_config["groups"].each { |group_info|
    group = group_info["group"]   # 模块分组 1-基础模块 2-Tab模块3-自定义模块
    modules = group_info["modules"]
    modules.each { |module_info|
      type = module_info["type"]  # 业务类型 1-登录,2-首页,3-场景,4-我的,5-application 100-其他
      name = module_info["name"]
      version = module_info["version"]
      depends = module_info["depends"]

      if group == 2
        @module_config.content["tabs"] << name
        puts "ODM Builder Add Tabs #{name}"
      end
      if type == 1
        @module_config.content["login"] = name
      elsif type == 5
        @module_config.content["application"] = name
      end

      default_name = default_module_name(type)
      default_skeleton = default_skeleton_name(type)

      default_biz = default_biz_kit_name(type)  # bizkit暂时不需要移除

      pod_add_module = (depends.nil? || depends.empty?) ? Array.new : depends
      pod_add_module << {"name" => name, "version" => version}

      pod_delete_module = Array.new
      pod_delete_module << {"name" => default_skeleton} unless default_skeleton.nil? || default_skeleton.empty?


      if !default_name.nil? && !default_name.empty?
        if default_biz.nil? || default_biz.empty?
          # 添加mix,mix到 default模块
          @module_config.add_mix(name, default_name) unless name == default_name
        else
          is_dependency = false
          depends.each {|item|
            if item["name"] == default_biz
              is_dependency = true
              break
            end
          } unless depends.nil? || depends.empty?
          if !is_dependency
            # 添加mix, mix到 biz模块 (可能是基于sdk开发)
            @module_config.add_mix(name, default_biz)
          end
          pod_delete_module << {"name" => default_name}
        end
      end

      podfile.update(nil, pod_delete_module) unless pod_delete_module.nil? || pod_delete_module.empty?  # 确保先删除查,再添加
      podfile.update(pod_add_module, nil) unless pod_add_module.nil? || pod_add_module.empty?


      puts "ODM Builder delteModule:#{pod_delete_module} addedModule:#{pod_add_module}".green unless (pod_delete_module.nil? || pod_delete_module.empty?) && (pod_add_module.nil? || pod_add_module.empty?)
    }
  }

  @module_config.content["tabSelect"] = @module_config.content["tabs"][0] unless @module_config.content["tabs"].nil? || @module_config.content["tabs"].empty?

  podfile.save

  puts "ODM builder config update finish".green
end

#validate(config) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/tuya/ci/core/odm_build.rb', line 61

def validate(config)
  result_bool = false
  if config.nil? || config.empty?
    raise "template_config is empty"
  elsif config["tid"] == 0
    raise "tid is necessary in template_config"
  elsif config["baseBranch"].nil? || config["baseBranch"].empty?
    raise "base branch is necessary in template_config"
  elsif config["branchName"].nil? || config["branchName"].empty?
    raise "branch name is necessary in template_config"
  else
    result_bool = true
  end
  puts "odm prebuild validate success".green if result_bool
  result_bool
end