Class: Pod::ProjectManipulator

Inherits:
Object
  • Object
show all
Defined in:
lib/yk_command/project/setup/ProjectManipulator.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ ProjectManipulator

Returns a new instance of ProjectManipulator.



12
13
14
15
16
17
18
# File 'lib/yk_command/project/setup/ProjectManipulator.rb', line 12

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

Instance Attribute Details

#configuratorObject (readonly)

Returns the value of attribute configurator.



6
7
8
# File 'lib/yk_command/project/setup/ProjectManipulator.rb', line 6

def configurator
  @configurator
end

#platformObject (readonly)

Returns the value of attribute platform.



6
7
8
# File 'lib/yk_command/project/setup/ProjectManipulator.rb', line 6

def platform
  @platform
end

#prefixObject (readonly)

Returns the value of attribute prefix.



6
7
8
# File 'lib/yk_command/project/setup/ProjectManipulator.rb', line 6

def prefix
  @prefix
end

#remove_demo_targetObject (readonly)

Returns the value of attribute remove_demo_target.



6
7
8
# File 'lib/yk_command/project/setup/ProjectManipulator.rb', line 6

def remove_demo_target
  @remove_demo_target
end

#string_replacementsObject (readonly)

Returns the value of attribute string_replacements.



6
7
8
# File 'lib/yk_command/project/setup/ProjectManipulator.rb', line 6

def string_replacements
  @string_replacements
end

#xcodeproj_pathObject (readonly)

Returns the value of attribute xcodeproj_path.



6
7
8
# File 'lib/yk_command/project/setup/ProjectManipulator.rb', line 6

def xcodeproj_path
  @xcodeproj_path
end

Class Method Details

.perform(options) ⇒ Object



8
9
10
# File 'lib/yk_command/project/setup/ProjectManipulator.rb', line 8

def self.perform(options)
  new(options).perform
end

Instance Method Details

#add_podspec_metadataObject



39
40
41
42
43
44
# File 'lib/yk_command/project/setup/ProjectManipulator.rb', line 39

def 
   = @project.root_object.main_group.children.select { |group| group.name == "Podspec Metadata" }.first
  .new_file "../" + @configurator.pod_name  + ".podspec"
  .new_file "../README.md"
  .new_file "../LICENSE"
end

#project_folderObject



86
87
88
# File 'lib/yk_command/project/setup/ProjectManipulator.rb', line 86

def project_folder
  File.dirname @xcodeproj_path
end

#remove_demo_projectObject



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
76
77
78
79
80
81
82
83
84
# File 'lib/yk_command/project/setup/ProjectManipulator.rb', line 46

def remove_demo_project
  app_project = @project.native_targets.find { |target| target.product_type == "com.apple.product-type.application" }
  test_target = @project.native_targets.find { |target| target.product_type == "com.apple.product-type.bundle.unit-test" }
  test_target.name = @configurator.pod_name + "_Tests"

  # Remove the implicit dependency on the app
  test_dependency = test_target.dependencies.first
  test_dependency.remove_from_project
  app_project.remove_from_project

  # Remove the build target on the unit tests
  test_target.build_configuration_list.build_configurations.each do |build_config|
    build_config.build_settings.delete "BUNDLE_LOADER"
  end

  # Remove the references in xcode
  project_app_group = @project.root_object.main_group.children.select { |group| group.display_name.end_with? @configurator.pod_name }.first
  project_app_group.remove_from_project

  # Remove the product reference
  product = @project.products.select { |product| product.path == @configurator.pod_name + "_Example.app" }.first
  product.remove_from_project

  # Remove the actual folder + files for both projects
  `rm -rf templates/ios/Example/PROJECT`
  `rm -rf templates/swift/Example/PROJECT`

  # Replace the Podfile with a simpler one with only one target
  podfile_path = project_folder + "/Podfile"
  podfile_text = <<-RUBY
use_frameworks!
target '#{test_target.name}' do
  pod '#{@configurator.pod_name}', :path => '../'
  
  ${INCLUDED_PODS}
end
RUBY
  File.open(podfile_path, "w") { |file| file.puts podfile_text }
end

#rename_filesObject



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/yk_command/project/setup/ProjectManipulator.rb', line 90

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

    # 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



120
121
122
123
124
# File 'lib/yk_command/project/setup/ProjectManipulator.rb', line 120

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



126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/yk_command/project/setup/ProjectManipulator.rb', line 126

def replace_internal_project_settings
  Dir.glob(project_folder + "/**/**/**/**").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



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/yk_command/project/setup/ProjectManipulator.rb', line 20

def run
  @string_replacements = {
    "PROJECT_OWNER" => @configurator.user_name,
    "TODAYS_DATE" => @configurator.date,
    "TODAYS_YEAR" => @configurator.year,
    "PROJECT" => @configurator.pod_name,
    "CPD" => @prefix
  }
  replace_internal_project_settings

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

  rename_files
  rename_project_folder
end