Class: TYCiCore::TemplateProject

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(configurator) ⇒ TemplateProject

Returns a new instance of TemplateProject.



9
10
11
12
13
14
15
# File 'lib/tuya/ci/core/template/template_project.rb', line 9

def initialize(configurator)
 @configurator = configurator
  @xcodeproj_path = configurator.xcodeproj_path
  @platform = configurator.platform
  @remove_demo_target = configurator.remove_demo_project
  @prefix = configurator.prefix
end

Instance Attribute Details

#platformObject (readonly)

Returns the value of attribute platform.



7
8
9
# File 'lib/tuya/ci/core/template/template_project.rb', line 7

def platform
  @platform
end

#prefixObject (readonly)

Returns the value of attribute prefix.



7
8
9
# File 'lib/tuya/ci/core/template/template_project.rb', line 7

def prefix
  @prefix
end

#remove_demo_targetObject (readonly)

Returns the value of attribute remove_demo_target.



7
8
9
# File 'lib/tuya/ci/core/template/template_project.rb', line 7

def remove_demo_target
  @remove_demo_target
end

#string_replacementsObject (readonly)

Returns the value of attribute string_replacements.



7
8
9
# File 'lib/tuya/ci/core/template/template_project.rb', line 7

def string_replacements
  @string_replacements
end

#xcodeproj_pathObject (readonly)

Returns the value of attribute xcodeproj_path.



7
8
9
# File 'lib/tuya/ci/core/template/template_project.rb', line 7

def xcodeproj_path
  @xcodeproj_path
end

Instance Method Details

#project_folderObject



33
34
35
# File 'lib/tuya/ci/core/template/template_project.rb', line 33

def project_folder
  File.dirname @xcodeproj_path
end

#rename_filesObject



37
38
39
40
41
42
43
44
45
46
47
48
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
# File 'lib/tuya/ci/core/template/template_project.rb', line 37

def rename_files
  # shared schemes have project specific names
  scheme_path = project_folder + "/PROJECT.xcodeproj/xcshareddata/xcschemes/"
  File.rename(scheme_path + "PROJECT.xcscheme", scheme_path +  @configurator.pod_name + "-Example.xcscheme")

  # rename xcproject
  File.rename(project_folder + "/PROJECT.xcodeproj", project_folder + "/" +  @configurator.pod_name + ".xcodeproj")

  unless @remove_demo_target
    # change app file prefixes
    ["CPDAppDelegate.h", "CPDAppDelegate.m", "CPDViewController.h", "CPDViewController.m"].each do |file|
      before = project_folder + "/PROJECT/" + file
      next unless File.exists? before

      after = project_folder + "/PROJECT/" + file.gsub("CPD", prefix)
      File.rename before, after
    end

    # add module impl
    ["PROJECTImpl.h", "PROJECTImpl.m"].each do |file|
      before = "#{@configurator.pod_name}/PROJECT/Classes/" + file

      next unless File.exists? before

      after = "#{@configurator.pod_name}/PROJECT/Classes/" + file.gsub("PROJECT", @configurator.pod_name)
      File.rename before, after
    end

    # rename project related files
    ["PROJECT-Info.plist", "PROJECT-Prefix.pch", "PROJECT.entitlements"].each do |file|
      before = project_folder + "/PROJECT/" + file
      next unless File.exists? before

      after = project_folder + "/PROJECT/" + file.gsub("PROJECT", @configurator.pod_name)
      File.rename before, after
    end
  end

end

#rename_project_folderObject



77
78
79
80
81
# File 'lib/tuya/ci/core/template/template_project.rb', line 77

def rename_project_folder
  if Dir.exist? project_folder + "/PROJECT"
    File.rename(project_folder + "/PROJECT", project_folder + "/" + @configurator.pod_name)
  end
end

#replace_internal_project_settingsObject



83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/tuya/ci/core/template/template_project.rb', line 83

def replace_internal_project_settings

  all_dir = Dir.glob(project_folder + "/**/**/**/**")# + Dir.glob("#{@configurator.pod_name}/**/**/**/**")

  all_dir.each do |name|
    next if Dir.exists? name
    text = File.read(name)
    for find, replace in @string_replacements
        text = text.gsub(find, replace)
    end

    File.open(name, "w") { |file| file.puts text }
  end
end

#runObject



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/tuya/ci/core/template/template_project.rb', line 17

def run
  @string_replacements = {
   "PROJECT_OWNER" => @configurator.owner_email,
    "PROJECT" => @configurator.pod_name,
    "CPD" => @configurator.prefix
  }

  replace_internal_project_settings

  @project = Xcodeproj::Project.open(@xcodeproj_path)
  @project.save

  rename_files
  rename_project_folder
end