Class: Pod::Command::Diff

Inherits:
Pod::Command show all
Defined in:
lib/cocoapods-diff/command/diff.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(argv) ⇒ Diff

Returns a new instance of Diff.



29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/cocoapods-diff/command/diff.rb', line 29

def initialize(argv)
  # Let's get all the command line arguments.
  @pod_name = argv.shift_argument
  @older_version = argv.shift_argument
  @newer_version = argv.shift_argument
  @use_regex = argv.flag?(CocoapodsDiff::REGEX_FLAG_NAME)
  @include_dependencies = argv.flag?(CocoapodsDiff::INCLUDE_DEPENDENCIES_FLAG_NAME)
  @platforms = argv.option(CocoapodsDiff::PLATFORMS_OPTION_NAME, "").split(",")
  @markdown_path = argv.option(CocoapodsDiff::MARKDOWN_OPTION_NAME)
  @newer_podfile_path = argv.option(CocoapodsDiff::NEWER_PODFILE_OPTION_NAME)
  @older_podfile_path = argv.option(CocoapodsDiff::OLDER_PODFILE_OPTION_NAME)
  @print_diff = @markdown_path.nil? && @newer_podfile_path.nil? && @older_podfile_path.nil?
  super
end

Class Method Details

.optionsObject



18
19
20
21
22
23
24
25
26
27
# File 'lib/cocoapods-diff/command/diff.rb', line 18

def self.options
  [
    ["--#{CocoapodsDiff::REGEX_FLAG_NAME}", "Interpret the `POD_NAME` as a regular expression"],
    ["--#{CocoapodsDiff::INCLUDE_DEPENDENCIES_FLAG_NAME}", "Include dependencies in diff."],
    ["--#{CocoapodsDiff::PLATFORMS_OPTION_NAME}", "Platforms to be compared. If not set, all platforms will be compared. Example: --#{CocoapodsDiff::PLATFORMS_OPTION_NAME}=ios,tvos"],
    ["--#{CocoapodsDiff::MARKDOWN_OPTION_NAME}", "Output a markdown file with diffs. Example: --#{CocoapodsDiff::MARKDOWN_OPTION_NAME}=path/to/save/markdown_name.md"],
    ["--#{CocoapodsDiff::NEWER_PODFILE_OPTION_NAME}", "Output a Podfile with the newer's versions. Example: --#{CocoapodsDiff::NEWER_PODFILE_OPTION_NAME}=path/to/save/Podfile_name"],
    ["--#{CocoapodsDiff::OLDER_PODFILE_OPTION_NAME}", "Output a Podfile with the older's versions. Example: --#{CocoapodsDiff::NEWER_PODFILE_OPTION_NAME}=path/to/save/Podfile_name"]
  ].concat(super)
end

Instance Method Details

#runObject



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
# File 'lib/cocoapods-diff/command/diff.rb', line 80

def run
  # As we already validate the arguments provided, it's safe to get the podspecs.
  older_spec = get_specification_for_version(@older_version)
  newer_spec = get_specification_for_version(@newer_version)

  # Warn the user if there's no subspecs or dependencies to compare.
  if older_spec.subspecs.empty? && older_spec.dependencies.empty? && newer_spec.subspecs.empty? && newer_spec.dependencies.empty?
    return UI.warn "There's nothing to compare for #{@pod_name} #{@older_version} vs. #{@newer_version}"
  end
  
  # Get all the supported platforms without the OS version if no platforms are specified
  @platforms = (newer_spec.available_platforms.map(&:name) | older_spec.available_platforms.map(&:name)) if @platforms.empty?
  @platforms.map! { |platform| Pod::Platform.new(platform) }

  # Let cocoapods resolve the dependencies for a spec
  @older_specs = resolve_dependencies_for_spec(older_spec)
  @newer_specs = resolve_dependencies_for_spec(newer_spec)
  
  # If no markdown or podfile options are passed, just print the diff on console
  if @print_diff
    return UI.title "Calculating diff" do
      UI.puts generate_diff_table
    end
  end

  if @markdown_path
    UI.title "Generating the Markdown file at #{@markdown_path}" do
      generate_markdown_file
    end
  end

  if @newer_podfile_path
    UI.title "Generating the Podfile for #{newer_spec.name} #{newer_spec.version} at #{@newer_podfile_path}" do
      generate_podfile_file(newer_spec, @newer_specs, @newer_podfile_path)
    end
  end

  if @older_podfile_path
    UI.title "Generating the Podfile for #{older_spec.name} #{older_spec.version} at #{@older_podfile_path}" do
      generate_podfile_file(older_spec, @older_specs, @older_podfile_path)
    end
  end
end

#validate!Object



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
# File 'lib/cocoapods-diff/command/diff.rb', line 44

def validate!
  super

  # Valdiate the required arguments.
  help! "A Pod name is required." unless @pod_name
  help! "An old Version is required." unless @older_version
  help! "A new Version is required." unless @newer_version
  help! "Versions should be different." if @older_version === @newer_version

  # Reverse versions if the newer version is smaller than the older one.
  @older_version, @newer_version = @newer_version, @older_version if Pod::Version.new(@newer_version) < Pod::Version.new(@older_version)

  # Validate that the Podspecs with the specified versions exist by using the `pod spec which` command.
  # Arguments needed for the command.
  args = [@pod_name]
  args += ["--#{CocoapodsDiff::REGEX_FLAG_NAME}"] if @use_regex

  # Podspec with the older version validation
  begin
    older_which_spec = Pod::Command::Spec::Which.new CLAide::ARGV.new(args + ["--version=#{@older_version}"])
    older_which_spec.run
  rescue Pod::Informative => e
    raise DiffInformative, "There was a problem trying to locate the pod #{@pod_name} (#{@older_version})\n" +
      "Original error message: #{e.message}"
  end

  # Podspec with the newer version validation
  begin
    newer_which_spec = Pod::Command::Spec::Which.new CLAide::ARGV.new(args + ["--version=#{@newer_version}"])
    newer_which_spec.run
  rescue Pod::Informative => e
    raise DiffInformative, "There was a problem trying to locate the pod #{@pod_name} (#{@newer_version})\n" +
      "Original error message: #{e.message}"
  end
end