Top Level Namespace

Defined Under Namespace

Modules: CocoapodsNativescript, Pod

Instance Method Summary collapse

Instance Method Details

#addfiles(direc, current_group, main_target) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/cocoapods-nativescript.rb', line 8

def addfiles (direc, current_group, main_target)
  Dir.glob(direc) do |item|
      next if item == '.' or item == '.DS_Store'

              if File.directory?(item)
          new_folder = File.basename(item)
          created_group = current_group.new_group(new_folder)
          addfiles("#{item}/*", created_group, main_target)
      else 
        i = current_group.new_file(item)
        if item.include? ".m"
            main_target.add_file_references([i])
        end
      end
  end
end

#command_restore_state(project_path) ⇒ Object



236
237
238
239
240
241
242
243
# File 'lib/cocoapods-nativescript.rb', line 236

def command_restore_state(project_path)
  puts "Restoring project state..."
  require 'xcodeproj'
  require 'json'
  project = Xcodeproj::Project.open(project_path)
  puts project
  restore_state(project)
end

#get_appdelegate_path(user_project) ⇒ Object



123
124
125
126
127
# File 'lib/cocoapods-nativescript.rb', line 123

def get_appdelegate_path(user_project)
  src_root = File.expand_path("..", user_project.path)
  app_delegate_path = File.expand_path('./App/AppDelegate.swift', src_root)
  return app_delegate_path
end

#get_ns_state_hash(user_project) ⇒ Object



149
150
151
152
153
154
# File 'lib/cocoapods-nativescript.rb', line 149

def get_ns_state_hash(user_project) 
  state_path = get_ns_state_path(user_project)
  file = File.read(state_path)
  state_hash = JSON.parse(file)
  return state_hash
end

#get_ns_state_path(user_project) ⇒ Object



143
144
145
146
147
# File 'lib/cocoapods-nativescript.rb', line 143

def get_ns_state_path(user_project)
  src_root = File.expand_path("..", user_project.path)
  state_path = File.expand_path('./pre-ns-state.json', src_root)
  return state_path
end

#get_podfile_path(user_project) ⇒ Object



133
134
135
136
137
# File 'lib/cocoapods-nativescript.rb', line 133

def get_podfile_path(user_project)
  src_root = File.expand_path("..", user_project.path)
  podfile_path = File.expand_path('./Podfile', src_root)
  return podfile_path
end

#nativescript_capacitor_post_install(installer) ⇒ Object



245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
# File 'lib/cocoapods-nativescript.rb', line 245

def nativescript_capacitor_post_install(installer)
  nativescript_post_install(installer)

  main_target = nil
  
  user_project = nil
  installer.aggregate_targets.each do |target|
      user_project = target.user_project
      user_project.build_configurations.each do |config|
          config.build_settings["SWIFT_OBJC_BRIDGING_HEADER"] = "$(SRCROOT)/NativeScript/App-Bridging-Header.h"
          config.build_settings["USER_HEADER_SEARCH_PATHS"] = "$(SRCROOT)/NativeScript"
      end
      main_target = user_project.targets.first
  
  end

  state_hash = get_ns_state_hash(user_project)

  #Add NativeScript group
  pods_path = File.expand_path("..", installer.pods_project.path)
  nativescript_dir_path = File.expand_path("../../../../node_modules/@nativescript/capacitor/embed/ios/NativeScript", installer.pods_project.path)
  src_root = File.expand_path("..", pods_path)
  ns_dir_dest = File.expand_path("NativeScript", src_root)

  FileUtils.copy_entry(nativescript_dir_path, ns_dir_dest)

  state_hash['added_dirs'] = [ns_dir_dest]

  ns_group = user_project.new_group('NativeScript')
  ns_path = ns_dir_dest
  addfiles("#{ns_path}/*", ns_group, main_target)

  state_hash['added_groups'] = ["NativeScript"]

  save_ns_state_hash(state_hash, user_project)

  user_project.save
  
end

#nativescript_post_install(installer) ⇒ Object



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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/cocoapods-nativescript.rb', line 26

def nativescript_post_install(installer)
  
    save_current_state(installer)
    
    pods_path = File.expand_path("..", installer.pods_project.path)
    internal_path = pods_path + "/NativeScript/resources"
    src_root = File.expand_path("..", pods_path)
    @internal_dest = File.expand_path("internal", src_root)
    FileUtils.copy_entry internal_path, @internal_dest 
    main_target = nil
    installer.aggregate_targets.each do |target|
        user_project = target.user_project
        user_project.build_configurations.each do |config|
   
            config.build_settings["ENABLE_BITCODE"] = "NO"
            config.build_settings["CLANG_ENABLE_MODULES"] = "NO"
            config.build_settings["LD"] = "$SRCROOT/internal/nsld.sh"
            config.build_settings["LDPLUSPLUS"] = "$SRCROOT/internal/nsld.sh"
            config.build_settings["OTHER_LDFLAGS"] = '$(inherited) -framework WebKit$(inherited) -ObjC -sectcreate __DATA __TNSMetadata $(CONFIGURATION_BUILD_DIR)/metadata-$(CURRENT_ARCH).bin -F $(SRCROOT)/internal -licucore -lz -lc++ -framework Foundation -framework UIKit -framework CoreGraphics -framework MobileCoreServices -framework Security'
        end
        main_target = user_project.targets.first
   
    end
   
   
   sources_index = nil
   main_target.build_phases.each_with_index do |phase, i|
     if phase.class.name == "Xcodeproj::Project::Object::PBXSourcesBuildPhase"
       sources_index = i
     end
   end
   
   pre_build = main_target.new_shell_script_build_phase("NativeScript PreBuild")
   pre_build.shell_script = "${SRCROOT}/internal/nativescript-pre-build"
   
   main_target.build_phases.move_from(main_target.build_phases.length-1, sources_index)
   
   link_index = nil
   main_target.build_phases.each_with_index do |phase, i|
     if phase.class.name == "Xcodeproj::Project::Object::PBXFrameworksBuildPhase"
       link_index = i
     end
   end
   
   pre_link = main_target.new_shell_script_build_phase("NativeScript PreLink")
   pre_link.shell_script = "${SRCROOT}/internal/nativescript-pre-link"
   
   main_target.build_phases.move_from(main_target.build_phases.length-1, link_index)
   
   
end

#nativescript_restore_state(installer) ⇒ Object



110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/cocoapods-nativescript.rb', line 110

def nativescript_restore_state(installer)
     file = File.read('./pre-ns-state.json')
     state_hash = JSON.parse(file)
     #restore settings
     user_project = nil
     installer.aggregate_targets.each do |target|
         user_project = target.user_project
   
     end
     
     restore_state(user_project) 
end

#restore_state(user_project) ⇒ Object



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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
# File 'lib/cocoapods-nativescript.rb', line 162

def restore_state(user_project)
  
    state_hash = get_ns_state_hash(user_project)
    main_target = user_project.targets.first
     
     user_project.build_configurations.each do |config|
       state_hash[config.name]["settings"].each do |key, value|
         if value
           config.build_settings[key] = value
           puts key
         else
           puts "deleting key " + key
           config.build_settings.delete(key)
         end
       end
     end
     
     main_target.build_phases.each do |phase|
       unless state_hash["main_target_build_phases"].include? phase.to_s
        puts "REMOVING PHASE:"
        puts phase.to_s
         phase.remove_from_project
       end
     end

    groups = state_hash["added_groups"]
    if groups && groups.length > 0 
      groups.each do |groupName|
        user_project.groups.each do |group|
          if group.name == groupName
            group.remove_from_project
          end
        end
      end
    end

    added_dirs = state_hash["added_dirs"]
    if added_dirs && added_dirs.length > 0
      added_dirs.each do |path|
        FileUtils.rm_rf(path)
      end
    end

    user_project.save

    podfile = File.read(get_podfile_path(user_project))
    podfile.slice! "require_relative '../../node_modules/@nativescript/capacitor/ios/nativescript.rb'"
    podfile.slice! "pod 'NativeScript'"
    podfile.slice! "pod 'NativeScriptUI'"
    podfile.slice! "nativescript_capacitor_post_install(installer)"

    save_podfile(podfile, user_project)

    appdelegate = File.read(get_appdelegate_path(user_project))
    to_slice = [
      "var nativescript: NativeScript?",
      "// NativeScript init",
      "let nsConfig = Config.init()",
      "nsConfig.metadataPtr = runtimeMeta()",
      "// can turn off in production",
      "nsConfig.isDebug = true",
      "nsConfig.logToSystemConsole = nsConfig.isDebug",
      'nsConfig.baseDir = URL(string: "public", relativeTo: Bundle.main.resourceURL)?.path',
      'nsConfig.applicationPath = "nativescript"',
      "self.nativescript = NativeScript.init(config: nsConfig)"
    ]
    
    to_slice.each do |line|
      appdelegate.slice! line
    end

    save_appdelegate(appdelegate, user_project)
end

#save_appdelegate(content, user_project) ⇒ Object



129
130
131
# File 'lib/cocoapods-nativescript.rb', line 129

def save_appdelegate(content, user_project)
  File.write(get_appdelegate_path(user_project), content)
end

#save_current_state(installer) ⇒ Object



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
108
# File 'lib/cocoapods-nativescript.rb', line 78

def save_current_state(installer)
     @state = Hash.new
   
     main_target = nil
     
     user_project = nil
     installer.aggregate_targets.each do |target|
         user_project = target.user_project
   
     end
     
     user_project.build_configurations.each do |config|
           @state[config.name] = Hash.new
           @state[config.name]["settings"] = {
             "ENABLE_BITCODE": config.build_settings["ENABLE_BITCODE"],
             "CLANG_ENABLE_MODULES": config.build_settings["ENABLE_BITCODE"],
             "LD": config.build_settings["LD"],
             "LDPLUSPLUS": config.build_settings["LDPLUSPLUS"],
             "OTHER_LDFLAGS": config.build_settings["OTHER_LDFLAGS"],
             "USER_HEADER_SEARCH_PATHS": config.build_settings["USER_HEADER_SEARCH_PATHS"],
             "SWIFT_OBJC_BRIDGING_HEADER": config.build_settings["SWIFT_OBJC_BRIDGING_HEADER"]
           }
     end
     
     main_target = user_project.targets.first
     @state["main_target_build_phases"] = main_target.build_phases
     json = @state.to_json
     
     File.write('./pre-ns-state.json', json)
     
end

#save_ns_state_hash(hash, user_project) ⇒ Object



156
157
158
159
160
# File 'lib/cocoapods-nativescript.rb', line 156

def save_ns_state_hash(hash, user_project)
  json = hash.to_json
  state_path = get_ns_state_path(user_project)
  File.write(state_path, json)
end

#save_podfile(content, user_project) ⇒ Object



139
140
141
# File 'lib/cocoapods-nativescript.rb', line 139

def save_podfile(content, user_project)
  File.write(get_podfile_path(user_project), content)
end