Class: Commands::Prepare

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

Instance Method Summary collapse

Instance Method Details

#optionsObject

holds the options that were passed you can set any initial defaults here



13
14
15
16
# File 'lib/commands/prepare.rb', line 13

def options
  @options ||= {
  }
end

#prepare_repo(repo, top_dir, initials, nobranch, with_local_version, shallow) ⇒ Object

prepare a single repo and create a local and tracking branch if it should have one if the repo specifies a branch then we will do the initial fetch from that branch if the create_dev_branch flag is set, we will create a local and tracking branch with the developer initials prepended



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
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/commands/prepare.rb', line 57

def prepare_repo(repo, top_dir, initials, nobranch, with_local_version, shallow)
  git_path = repo[:git_path]
  branch = repo[:branch]
  create_dev_branch = (repo[:create_dev_branch] == true) && (!nobranch)
  tag = repo[:tag]
  parent_path = repo[:parent_path]

  if tag && branch
    raise "We only support branching relative to HEAD, you cannot specify a branch:#{branch} and a tag:#{tag}."
  end

  if tag && create_dev_branch
    raise "We do not support creating dev branches when using a tag"
  end

  # build the parent path if we have one and make it the new top dir
  if parent_path
    Dir.chdir(top_dir){
      FileUtils.mkpath(parent_path)
    }
    top_dir = "#{top_dir}/#{parent_path}"
  end

  # first clone the repo
  cmd = "git clone #{git_path} -b #{branch} #{shallow}"
  if EbmSharedLib::CL.do_cmd_result(cmd, top_dir) != 0
    raise "Cloning repo at #{git_path} failed."
  end

  # extract the directory name that this repo ended up in
  repo_name = EbmSharedLib.get_repo_name(git_path)
  repo_path = "#{top_dir}/#{repo_name}"

  # now set up the remote tracking base branch if we have one
  if branch
    cmd = "git checkout -B #{branch} --track origin/#{branch}"
    if EbmSharedLib::CL.do_cmd_result(cmd, repo_path) != 0
      raise "Checking out branch #{branch} for #{repo_name} failed."
    end

    # now create the developer branch off of the base branch
    if create_dev_branch
      dev_branch_name = "#{initials}_#{branch}"
      # first assume dev branch already exists and try to switch to it
      cmd = "git checkout --track origin/#{dev_branch_name}"
      if EbmSharedLib::CL.do_cmd_result(cmd, repo_path) != 0
        # no remote branch, so create local and push as remote
        cmd = "git checkout -B #{dev_branch_name}"
        if EbmSharedLib::CL.do_cmd_result(cmd, repo_path) != 0
          raise "Unable to create local and tracking developer branch #{dev_branch_name} for #{repo_name}."
        end
        cmd = "git push -u origin #{dev_branch_name}"
        if EbmSharedLib::CL.do_cmd_result(cmd, repo_path) != 0
          raise "Unable to create local and tracking developer branch #{dev_branch_name} for #{repo_name}."
        end
      end
      if with_local_version == false
        # they don't want the local version branch so drop it
        cmd = "git branch -D #{branch}"
        if EbmSharedLib::CL.do_cmd_result(cmd, repo_path) != 0
          raise "Unable to delete local version branch #{branch} for #{repo_name}."
        end
      end
    end
  elsif tag
    # they are using a tag so don't create dev branches
    cmd = "git checkout #{tag}"
    if EbmSharedLib::CL.do_cmd_result(cmd, repo_path) != 0
      raise "Unable to checkout tag:#{tag} for #{repo_name}."
    end
  end

  # pull any submodules
  cmd = "git submodule init"
  if EbmSharedLib::CL.do_cmd_result(cmd, repo_path) != 0
    raise "Cloning submodules in repo at #{git_path} failed."
  end
  cmd = "git submodule update"
  if EbmSharedLib::CL.do_cmd_result(cmd, repo_path) != 0
    raise "Cloning submodules in repo at #{git_path} failed."
  end

end

#register(opts, global_options) ⇒ Object



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
# File 'lib/commands/prepare.rb', line 25

def register(opts, global_options)
  opts.banner = "Usage: prepare [options]"
  opts.description = "Prepare your build environment"

  opts.on('-r', "--config-repo name", EbmSharedLib::REPO_COMMAND_DETAILS) do |v|
    options[:config_repo] = v
  end
  
  opts.on('-c', "--config name", "Required - Name of the config we are preparing from.") do |v|
    options[:config] = v
  end

  opts.on('-i', "--initials name", "Initials prepended to your branch For example --initials gws will result in something like gws_iphone_3.0.") do |v|
    options[:initials] = v
  end

  opts.on("--no-branch", "Don't create developer branch.") do |v|
    options[:nobranch] = true # just indicate that the option was set (parms used --no to mean false but we don't want that)
  end

  opts.on("--with-local-version", "Also include a local version branch, only applies with the -i option.") do |v|
    options[:with_local_version] = true
  end
  opts.on('-n', "--shallow", "Checkout only level one to only have first level of history") do |v|
    options[:shallow] = true
  end
end

#required_optionsObject

required options



19
20
21
22
23
# File 'lib/commands/prepare.rb', line 19

def required_options
  @required_options ||= Set.new [
      :config,
  ]
end

#run(global_options) ⇒ Object



141
142
143
144
145
146
147
148
149
150
151
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
186
187
188
# File 'lib/commands/prepare.rb', line 141

def run(global_options)

  # see if we can open the config file - we append the .config suffix
  # the file is expected to be in JSON format
  config_name = options[:config]
  initials = options[:initials]
  nobranch = !!options[:nobranch]
  with_local_version = !!options[:with_local_version]
  shallow = !!options[:shallow] ? "--depth=1" : ""

  if (nobranch && initials)
    raise "You must specify either the --initials or --no-branch, not both."
  end

  if (initials.nil? && nobranch == false)
    raise "You must specify either --initials or --no-branch"
  end

  if (with_local_version && initials.nil?)
    raise "Using --with-local-version is only applicable if you are also creating a developer branch -i."
  end

  # try to make sure the repo is available
  config_repo = options[:config_repo]
  config_repo_url = EbmSharedLib.prepare_config_repo(config_repo)
  info = EbmSharedLib.read_repo_config(config_repo_url, config_name)

  # Now that we have the json, prepare the world by creating a directory with the config name and placing
  # the various repos beneath that.  The directory is created relative to the current directory.

  top_dir = "#{Dir.pwd}/#{config_name}"
  raise "Cannot prepare since top level directory already exists: #{top_dir}" if File.directory?(top_dir)

  Dir::mkdir(top_dir)

  repos = info[:repos]
  repos.each do |repo|
    prepare_repo(repo, top_dir, initials, nobranch, with_local_version, shallow)
  end

  # finish up by writing settings
  settings = {
      :config_repo_url => config_repo_url,
      :config_name => config_name,
  }
  EbmSharedLib.write_settings(settings, top_dir, err_msg = nil)

end