Class: Zewo::App::Repo

Inherits:
Object
  • Object
show all
Defined in:
lib/zewo.rb

Constant Summary collapse

@@repos =
{}
@@lock_branches =
{'CURIParser' => '0.2.0', 'CHTTPParser' => '0.2.0', 'CLibvenice' => '0.2.0'}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(repo, organization) ⇒ Repo

Returns a new instance of Repo.



29
30
31
32
# File 'lib/zewo.rb', line 29

def initialize(repo, organization)
  @repo = repo
  @organization = organization
end

Instance Attribute Details

#organizationObject (readonly)

Returns the value of attribute organization.



23
24
25
# File 'lib/zewo.rb', line 23

def organization
  @organization
end

#repoObject (readonly)

Returns the value of attribute repo.



21
22
23
# File 'lib/zewo.rb', line 21

def repo
  @repo
end

Class Method Details

.lock_branchesObject



269
270
271
# File 'lib/zewo.rb', line 269

def self::lock_branches
  @@lock_branches
end

.reposObject

getter/setter for class variable



274
275
276
# File 'lib/zewo.rb', line 274

def self::repos
  @@repos
end

.repos=(value) ⇒ Object



278
279
280
281
# File 'lib/zewo.rb', line 278

def self::repos=(value)
  puts @@repos
  @@repos = value
end

Instance Method Details

#add_files(direc, current_group, main_target) ⇒ Object



187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
# File 'lib/zewo.rb', line 187

def add_files(direc, current_group, main_target)
  Dir.glob(direc) do |item|
    next if item.start_with?('.')

    if File.directory?(item)
      new_folder = File.basename(item)
      created_group = current_group.new_group(new_folder)
      add_files("#{item}/*", created_group, main_target)
    else
      # Basically means "Remove Zewo from path and prepend '../'"
      item = item.split('/')[1..-1].unshift('..', '..') * '/'
      i = current_group.new_file(item)
      main_target.add_file_references([i]) if item.include? '.swift'
    end
  end
end

#branchObject



241
242
243
# File 'lib/zewo.rb', line 241

def branch
  call('git rev-parse --abbrev-ref HEAD')
end

#call(str) ⇒ Object



216
217
218
219
220
# File 'lib/zewo.rb', line 216

def call(str)
  output = `cd #{path}; #{str} 2>&1`.chomp
  raise output unless $?.success?
  output
end

#cloneObject



34
35
36
37
38
39
40
41
42
43
44
# File 'lib/zewo.rb', line 34

def clone
  puts "Cloning #{path}".green

  flags = ''

  if @@lock_branches[@repo]
    flags = "--branch #{@@lock_branches[@repo]}"
  end
  
  `git clone #{flags} https://github.com/#{@organization}/#{@repo} #{path} &> /dev/null`
end

#clone_dependenciesObject



70
71
72
73
# File 'lib/zewo.rb', line 70

def clone_dependencies
  # clone all dependencies that don't exist yet
  dependencies.each(&:clone_dependencies)
end

#configure_xcode_projectObject



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
# File 'lib/zewo.rb', line 152

def configure_xcode_project
  return if @configured

  puts "Configuring Xcode project for #{path}".green

  group = @xcode_project.new_group('Subprojects')

  # This next block will move through the flattened dependencies of a project
  # to determine whether any of them have headers associated with their module maps.
  # If they do, each header must be added to the header search paths configuration
  flat_dependencies.select { |d| d.headers.count > 0 }.each do |header_dep|
    header_search_path = "../../../#{header_dep.path}"

    [framework_target, test_target].each do |target|
      target.build_configurations.each do |configuration|
        existing = target.build_settings(configuration.name)['HEADER_SEARCH_PATHS']
        unless existing.include? header_search_path
          existing += " #{header_search_path}"
          target.build_settings(configuration.name)['HEADER_SEARCH_PATHS'] = existing
        end
      end
    end
  end

  dependencies.each do |repo|
    next if repo.repo.end_with?('-OSX')

    project_reference = group.new_file(repo.xcode_project_path.to_s)
    project_reference.path = "../../../#{project_reference.path}"
    framework_target.add_dependency(repo.framework_target) if framework_target
  end
  @xcode_project.save
  @configured = true
end

#configure_xcode_projectsObject



83
84
85
86
87
88
89
# File 'lib/zewo.rb', line 83

def configure_xcode_projects
  # configure xcode project
  configure_xcode_project

  # recursively do the same for all dependencies
  dependencies.each(&:configure_xcode_projects)
end

#dependenciesObject



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/zewo.rb', line 46

def dependencies
  clone unless File.directory?(path)

  package_swift_contents = File.read("#{path}/Package.swift")

  # matches the packages like `VeniceX/Venice`
  regex = /https:\/\/github.com\/(.*\/*)"/
  matches = package_swift_contents.scan(regex).map(&:first).map { |e| e = e.chomp('.git') if e.end_with?('.git'); e }

  # splits VeniceX/Venice into ['VeniceX', 'Venice']
  splits = matches.map { |e| e.split('/', 2) }

  # creates a Repo using VeniceX as organization and Venice as repo
  repos = splits.map { |s| Repo.new(s[1], s[0]) }

  cached = repos.map do |m|
    # add it to global list of repositories if it isnt already in there
    @@repos["#{m.organization}/#{m.repo}"] = m unless @@repos["#{m.organization}/#{m.repo}"]
    @@repos["#{m.organization}/#{m.repo}"]
  end

  cached
end

#flat_dependenciesObject



91
92
93
94
95
96
97
98
99
# File 'lib/zewo.rb', line 91

def flat_dependencies
  result = dependencies

  dependencies.each do |dep|
    result += dep.flat_dependencies
  end

  result.uniq
end

#framework_targetObject



254
255
256
257
258
# File 'lib/zewo.rb', line 254

def framework_target
  target_name = repo.gsub('-OSX', '').gsub('-', '_')
  target_name = 'OperatingSystem' if target_name == 'OS'
  @xcode_project.native_targets.find { |t| t.name == target_name } || @xcode_project.new_target(:framework, target_name, :osx)
end

#headersObject



102
103
104
# File 'lib/zewo.rb', line 102

def headers
  @headers ||= Dir.glob("#{path}/*.h")
end

#master_branch?Boolean

Returns:

  • (Boolean)


231
232
233
# File 'lib/zewo.rb', line 231

def master_branch?
  call('git rev-parse --abbrev-ref HEAD')
end

#pathObject



208
209
210
# File 'lib/zewo.rb', line 208

def path
  "#{@organization}/#{@repo}"
end

#pullObject



245
246
247
# File 'lib/zewo.rb', line 245

def pull
  call('git pull')
end

#setup_xcode_projectObject



106
107
108
109
110
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/zewo.rb', line 106

def setup_xcode_project
  return if @xcode_project

  puts "Creating Xcode project for #{@organization}/#{@repo}".green

  @xcode_project = Xcodeproj::Project.new("#{xcode_project_path}")
  @xcode_project.initialize_from_scratch

  framework_target.build_configurations.each do |configuration|
    framework_target.build_settings(configuration.name)['HEADER_SEARCH_PATHS'] = '/usr/local/include'
    framework_target.build_settings(configuration.name)['LIBRARY_SEARCH_PATHS'] = '/usr/local/lib'
    framework_target.build_settings(configuration.name)['ENABLE_TESTABILITY'] = 'YES'

    if File.exist?("#{path}/module.modulemap")
      framework_target.build_settings(configuration.name)['MODULEMAP_FILE'] = '../module.modulemap'
    end
  end

  if sources_path
    group = @xcode_project.new_group('Sources')
    add_files("#{path}/#{sources_path}/*", group, framework_target)
  end

  test_target.resources_build_phase
  test_target.add_dependency(framework_target)

  test_target.build_configurations.each do |configuration|
    test_target.build_settings(configuration.name)['WRAPPER_EXTENSION'] = 'xctest'
    test_target.build_settings(configuration.name)['LD_RUNPATH_SEARCH_PATHS'] = '$(inherited) @executable_path/../../Frameworks @loader_path/../Frameworks'
    test_target.build_settings(configuration.name)['HEADER_SEARCH_PATHS'] = '/usr/local/include'
    test_target.build_settings(configuration.name)['LIBRARY_SEARCH_PATHS'] = '/usr/local/lib'
  end

  group = @xcode_project.new_group('Tests')
  add_files("#{path}/Tests/*", group, test_target)

  @xcode_project.save

  scheme = Xcodeproj::XCScheme.new
  scheme.configure_with_targets(framework_target, test_target)
  # scheme.test_action.code_coverage_enabled = true
  scheme.save_as(@xcode_project.path, framework_target.name, true)

  framework_target.frameworks_build_phase.clear
end

#setup_xcode_projectsObject



75
76
77
78
79
80
81
# File 'lib/zewo.rb', line 75

def setup_xcode_projects
  # create xcode project
  setup_xcode_project

  # recursively do the same for all dependencies
  dependencies.each(&:setup_xcode_projects)
end

#sources_pathObject



249
250
251
252
# File 'lib/zewo.rb', line 249

def sources_path
  return 'Sources' if File.directory?("#{path}/Sources")
  return 'Source'  if File.directory?("#{path}/Source")
end

#statusObject



212
213
214
# File 'lib/zewo.rb', line 212

def status
  `cd #{path}; git status`
end

#tagObject



235
236
237
238
239
# File 'lib/zewo.rb', line 235

def tag
  return call('git describe --abbrev=0 --tags')
rescue
  return 'No tags'
end

#tag_lockObject



265
266
267
# File 'lib/zewo.rb', line 265

def tag_lock
  @tag_lock ||= @@lock_branches[@repo]
end

#test_targetObject



260
261
262
263
# File 'lib/zewo.rb', line 260

def test_target
  target_name = "#{framework_target.name}-Test"
  @xcode_project.native_targets.find { |t| t.name == target_name } || @xcode_project.new_target(:bundle, target_name, :osx)
end

#uncommited_changes?Boolean

Returns:

  • (Boolean)


222
223
224
225
226
227
228
229
# File 'lib/zewo.rb', line 222

def uncommited_changes?
  begin
    call('git diff --quiet HEAD')
    return false
  rescue
    return true
  end
end

#xcode_project_pathObject



204
205
206
# File 'lib/zewo.rb', line 204

def xcode_project_path
  "#{path}/XcodeDevelopment/#{@repo}.xcodeproj"
end