aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/tools/xcode-common/java/com/google/devtools/build/xcode/zippingoutput/ArgumentsParsing.java
blob: b7b9da26dcd23b4be27a54711183be5d2696fb3c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
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
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
// Copyright 2014 The Bazel Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//    http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package com.google.devtools.build.xcode.zippingoutput;

import com.google.common.base.Optional;
import com.google.common.collect.ImmutableList;
import com.google.devtools.build.xcode.util.Value;

import java.nio.file.FileSystem;
import java.nio.file.Files;
import java.util.Locale;

/**
 * Arguments passed to the do-something-then-zip wrapper tool.
 */
class ArgumentsParsing extends Value<ArgumentsParsing> {

  private final Optional<String> error;
  private final Optional<Arguments> arguments;

  private ArgumentsParsing(Optional<String> error, Optional<Arguments> arguments) {
    super(error, arguments);
    this.error = error;
    this.arguments = arguments;
  }

  public Optional<String> error() {
    return error;
  }

  public Optional<Arguments> arguments() {
    return arguments;
  }

  private static final int MIN_ARGS = 3;

  /**
   * @param args raw arguments passed to wrapper tool through the {@code main} method
   * @param subtoolName name of the subtool, such as "actool"
   * @return an instance based on results of parsing the given arguments.
   */
  public static ArgumentsParsing parse(FileSystem fileSystem, String[] args, String wrapperName,
      String subtoolName) {
    String capitalizedSubtool = subtoolName.toUpperCase(Locale.US);
    if (args.length < MIN_ARGS) {
      return new ArgumentsParsing(Optional.of(String.format(
              "Expected at least %1$d args.\n"
                  + "Usage: java %2$s OUTZIP ARCHIVEROOT (%3$s_CMD %3$s ARGS)\n"
                  + "Runs %4$s and zips the results.\n"
                  + "OUTZIP - the path to place the output zip file.\n"
                  + "ARCHIVEROOT - the path in the zip to place the output, or an empty\n"
                  + "    string for the root of the zip. e.g. 'Payload/foo.app'. If\n"
                  + "    this tool outputs a single file, ARCHIVEROOT is the name of\n"
                  + "    the only file in the zip file.\n"
                  + "%3$s_CMD - path to the subtool.\n"
                  + "    e.g. /Applications/Xcode.app/Contents/Developer/usr/bin/actool\n"
                  + "%3$s ARGS - the arguments to pass to %4$s besides the\n"
                  + "    one that specifies the output directory.\n",
              MIN_ARGS, wrapperName, capitalizedSubtool, subtoolName)),
          Optional.<Arguments>absent());
    }
    String outputZip = args[0];
    String archiveRoot = args[1];
    String subtoolCmd = args[2];
    if (archiveRoot.startsWith("/")) {
      return new ArgumentsParsing(
          Optional.of(String.format("Archive root cannot start with /: '%s'\n", archiveRoot)),
          Optional.<Arguments>absent());
    }

    // TODO(bazel-team): Remove this hack when the released version of Bazel uses the correct momc
    // path for device builds.
    subtoolCmd = subtoolCmd.replace("/iPhoneOS.platform/", "/iPhoneSimulator.platform/");
    if (!Files.isRegularFile(fileSystem.getPath(subtoolCmd))) {
      return new ArgumentsParsing(
          Optional.of(String.format(
              "The given %s_CMD does not exist: '%s'\n", capitalizedSubtool, subtoolCmd)),
          Optional.<Arguments>absent());
    }
    return new ArgumentsParsing(
        Optional.<String>absent(),
        Optional.<Arguments>of(
            new Arguments(
                outputZip, archiveRoot, subtoolCmd,
                ImmutableList.copyOf(args).subList(MIN_ARGS, args.length))));
  }
}