Module: Podspec

Defined in:
lib/podspec/cli.rb,
lib/podspec.rb,
lib/podspec/spec.rb,
lib/podspec/parse.rb,
lib/podspec/version.rb

Overview

Version, constants

Constant Summary collapse

DESC =
"# s.description  = \"A description of the Pod more detailed than the summary.\""
IOS =
'8.0'
OSX =
'10.9'
WATCHOS =
'2.0'
TVOS =
'9.0'
SOURCE_FILES =
[
  'Sources'
]
VERSION =
'0.3.0'
PRODUCT =
'podspec'

Class Method Summary collapse

Class Method Details

.cliObject



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
# File 'lib/podspec/cli.rb', line 13

def cli
  elapsed_time_start = Time.now

  puts "#{PRODUCT} #{VERSION}"

  if ARGV.count == 0
    usage
    exit
  end

  repo = ARGV[0].sub('https://github.com/', '')

  unless repo.include? '/'
    usage
    exit
  end

  parsed = parse repo

  e = parsed['error']
  unless e.nil?
    puts "Error: #{e}"
    exit
  end

  w = parsed['warnings']
  w.each do |x|
    puts "Warning: #{x}"
  end

  s = create_spec parsed

  filename = "#{parsed['name']}.podspec"
  File.open(filename, 'w') { |f| f.write(s) }
  elapsed_seconds = Time.now - elapsed_time_start

  puts "Wrote #{filename} in #{elapsed_seconds.round}s ✨"

  puts %{
• You can use an alternative author / authors format
• The deployment target(s) should be edited manually
Details → https://guides.cocoapods.org/syntax/podspec.html
}
end

.create_spec(s) ⇒ Object



110
111
112
113
# File 'lib/podspec/spec.rb', line 110

def create_spec(s)
  options = make_options s
  format options
end

.description(d, s) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/podspec/spec.rb', line 11

def description(d, s)
  description = DESC

  if d.nil?
    description = DESC
  else
    description =
      if d == s
        DESC
      else
        "s.description  = \"#{escape d}\""
      end
  end

  description
end

.escape(s) ⇒ Object



28
29
30
31
# File 'lib/podspec/spec.rb', line 28

def escape(s)
  return '' if s.nil?
  s.gsub('"', '\"')
end

.format(options) ⇒ Object



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
# File 'lib/podspec/spec.rb', line 33

def format(options)
  name = options['name']
  version = options['version']
  summary = options['summary']
  description = options['description']
  homepage = options['homepage']
  license = options['license']
  author = options['author']
  source = options['source']
  source_files = options['source_files']
  ios = options['ios']
  osx = options['osx']
  watchos = options['watchos']
  tvos = options['tvos']

  %{Pod::Spec.new do |s|
    s.name         = "#{name}"
    s.version      = "#{version}"
    s.summary      = "#{summary}"
    #{description}

    s.homepage     = "#{homepage}"

    s.license      = "#{license}"

    s.author       = "#{author}"

    s.source       = #{source}

    s.source_files = "#{source_files}"

    s.ios.deployment_target = "#{ios}"
    # s.osx.deployment_target = "#{osx}"
    # s.watchos.deployment_target = "#{watchos}"
    # s.tvos.deployment_target = "#{tvos}"
  end
  }
end

.homepage(s) ⇒ Object



72
73
74
75
76
# File 'lib/podspec/spec.rb', line 72

def homepage(s)
  homepage = s['homepage']
  homepage = s['html_url'] if homepage.nil? || homepage==''
  homepage
end

.make_options(s) ⇒ Object



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/podspec/spec.rb', line 82

def make_options(s)
  homepage = homepage s

  git = s['git'].sub('git://', 'https://')

  license = escape s['license']

  source_files = source_files s

  summary = s['summary']

  {
    'name' => s['name'],
    'version' => s['version'],
    'summary' => escape(summary),
    'description' => description(s['description'], summary),
    'homepage' => homepage,
    'license' => escape(s['license']),
    'author' => s['author'],
    'source' => "{ :git => \"#{git}\", :tag => \"#{s['tag']}\" }",
    'source_files' => source_files,
    'ios' => IOS,
    'osx' => OSX,
    'watchos' => WATCHOS,
    'tvos' => TVOS
  }
end

.parse(repo) ⇒ Object



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
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
85
86
87
88
89
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
119
120
121
122
123
124
125
126
127
128
# File 'lib/podspec/parse.rb', line 20

def parse(repo)
  puts "Generating Podspec for #{repo}..."

  begin
    c = Octokit
  rescue => e
    return {
      'error' => e
    }
  end

  begin
    r = c.repo(repo, :accept => 'application/vnd.github.drax-preview+json')

    lang = r['language']
    unless (lang == 'Swift') || (lang == 'Objective-C')
      return {
        'error' => "#{lang} is not supported"
      }
    end

    name = r['name']
    summary = r['description']

    begin
      license = r['license']['name']
    rescue
      license = nil
    end

    begin
      tags = c.tags(repo)
    rescue => e
      return {
        'error' => e
      }
    end

    if c.tags(repo).count > 0
      t = tags.sort do |a,b|
        v1 = tag a['name']
        v2 = tag b['name']
        Gem::Version.new(v2) <=> Gem::Version.new(v1)
      end[0]

      tag = t['name']
    else
      tag = 'TODO'
    end

    version = ver(tag)

    contents = c.contents repo
    folders = contents.select { |x| x['type']=='dir'}.map { |f| f['name'] }

    source_files = SOURCE_FILES
    source_files.push name
    intersect = source_files & folders
    source_folder = intersect.count == 0 ? nil : intersect[0]

    warnings = []
    warnings.push 'No sources found' if source_folder.nil?

    contents.select { |x| x['type']=='file'}
    .map { |f| f['name'] }
    .each do |x|
      warnings.push "A Podspec already exists for #{repo}" if x.include? 'podspec'
    end

    html_url = r['html_url']
    homepage = r['homepage']

    username = r['owner']['login']
    u = c.user username
    author = u['name']

    git = r['git_url']

    r = GitHubReadme::get repo
    if r['error'].nil?
      readme = r['readme']
      description = r['summary']
    else
      readme = nil
      description = nil
    end

    return {
      'error' => nil,
      'name' => name,
      'summary' => summary,
      'description' => description,
      'license' => license,
      'git' => git,
      'tag' => tag,
      'version' => version,
      'author' => author,
      'source_folder' => source_folder,
      'html_url' => html_url,
      'homepage' => homepage,
      'readme' => readme,
      'warnings' => warnings
    }
  rescue => e
    return {
      'error' => e
    }
  end
end

.source_files(s) ⇒ Object



78
79
80
# File 'lib/podspec/spec.rb', line 78

def source_files(s)
  "#{s['source_folder']}/*.{h,m,swift}"
end

.tag(name) ⇒ Object



10
11
12
# File 'lib/podspec/parse.rb', line 10

def tag(name)
  name.gsub(/[A-z]/, '').gsub('-','')
end

.usageObject



9
10
11
# File 'lib/podspec/cli.rb', line 9

def usage
  puts "Usage: #{PRODUCT} <GitHub Repo>\n  i.e. #{PRODUCT} postmates/PMJSON"
end

.ver(t) ⇒ Object



14
15
16
17
18
# File 'lib/podspec/parse.rb', line 14

def ver(t)
  return t if t=='TODO'

  t.gsub(/[a-zA-Z]/, '')
end