Chromium Code Reviews
|
| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 // This file provides a command-line interface to | |
| 6 // upgrade_test::GenerateAlternateVersion(). | |
| 7 | |
| 8 #include <cstdio> | |
| 9 #include <cstdlib> | |
| 10 | |
| 11 #include "base/at_exit.h" | |
| 12 #include "base/basictypes.h" | |
| 13 #include "base/command_line.h" | |
| 14 #include "base/file_path.h" | |
| 15 #include "base/file_util.h" | |
| 16 #include "base/logging.h" | |
| 17 #include "base/path_service.h" | |
| 18 #include "chrome/installer/test/alternate_version_generator.h" | |
| 19 | |
| 20 namespace { | |
| 21 | |
| 22 const wchar_t kMiniInstallerExe[] = L"mini_installer.exe"; | |
|
robertshield
2011/01/28 18:36:22
Have the word "default" in the names of these two
grt (UTC plus 2)
2011/01/28 19:13:33
Done.
| |
| 23 const wchar_t kMiniInstallerNewExe[] = L"mini_installer_new.exe"; | |
| 24 | |
| 25 namespace switches { | |
| 26 | |
| 27 const char kHelp[] = "help"; | |
| 28 const char kMiniInstaller[] = "mini_installer"; | |
| 29 const char kOut[] = "out"; | |
| 30 const char kPrevious[] = "previous"; | |
| 31 | |
| 32 } // namespace switches | |
| 33 | |
| 34 namespace errors { | |
| 35 | |
| 36 enum ErrorCode { | |
| 37 SHOW_HELP, | |
| 38 MINI_INSTALLER_NOT_FOUND, | |
| 39 OUT_FILE_EXISTS, | |
| 40 GENERATION_FAILED | |
| 41 }; | |
| 42 | |
| 43 const wchar_t* const Messages[] = { | |
| 44 NULL, | |
| 45 L"original mini_installer.exe not found", | |
| 46 L"output file already exists", | |
| 47 L"failed to generate a newly versioned mini_installer.exe" | |
| 48 }; | |
| 49 | |
| 50 const wchar_t* GetErrorMessage(ErrorCode error_code) { | |
| 51 DCHECK_LE(0, error_code); | |
| 52 DCHECK_GT(arraysize(Messages), static_cast<size_t>(error_code)); | |
| 53 return Messages[error_code]; | |
| 54 } | |
| 55 | |
| 56 } // namespace errors | |
| 57 | |
| 58 // Display usage information to stderr along with an optional error message with | |
| 59 // details. | |
| 60 void DumpUsage(const CommandLine& cmd_line, | |
| 61 errors::ErrorCode error_code, | |
| 62 const std::wstring& detail) { | |
| 63 const wchar_t* error_message = errors::GetErrorMessage(error_code); | |
| 64 if (error_message != NULL) { | |
| 65 fwprintf(stderr, L"%s: %s", cmd_line.GetProgram().value().c_str(), | |
| 66 errors::GetErrorMessage(error_code)); | |
| 67 if (!detail.empty()) | |
| 68 fwprintf(stderr, L" (%s)\n", detail.c_str()); | |
| 69 else | |
| 70 fwprintf(stderr, L"\n"); | |
| 71 } | |
| 72 | |
| 73 fwprintf(stderr, | |
| 74 L"Usage: %s [ --mini_installer=SRC_PATH ] [--out=OUT_PATH ] [ --previous ]\n" | |
|
robertshield
2011/01/28 18:36:22
Does this line wrap properly on the command line?
grt (UTC plus 2)
2011/01/28 19:13:33
Done.
| |
| 75 L" [ --7za_path=7ZA_PATH ]\n" | |
| 76 L" --help Display this help message.\n" | |
| 77 L" --mini_installer=SRC_PATH Relative or absolute path to mini_installer.exe.\n " // NOLINT | |
|
grt (UTC plus 2)
2011/01/28 16:49:53
some of these are longer than 80 cols since the te
robertshield
2011/01/28 18:36:22
Prefer 2) as the option that provides the most rea
grt (UTC plus 2)
2011/01/28 19:13:33
Done.
| |
| 78 L" Default value is \"mini_installer.exe\" in the sam e\n" // NOLINT | |
| 79 L" directory as this program.\n" | |
| 80 L" --out=OUT_PATH Relative or absolute path to output file. Default\ n" // NOLINT | |
| 81 L" value is \"mini_installer_new.exe\" in the current \n" // NOLINT | |
| 82 L" directory.\n" | |
| 83 L" --previous OUT_PATH will have a lower version than SRC_PATH.\ n" // NOLINT | |
| 84 L" By default, OUT_PATH will have a higher version.\n " // NOLINT | |
| 85 L" --7za_path=7ZA_PATH Relative or absolute path to the directory holding \n" // NOLINT | |
| 86 L" 7za.exe. Default value is\n" | |
| 87 L" ..\\..\\third_party\\lzma_sdk\\Executable relative to\n" // NOLINT | |
| 88 L" the directory holding this program.", | |
| 89 cmd_line.GetProgram().value().c_str()); | |
| 90 } | |
| 91 | |
| 92 // Gets the path to the source mini_installer.exe on which to operate, putting | |
| 93 // the result in |mini_installer|. Returns true on success. | |
| 94 bool GetMiniInstallerPath(const CommandLine& cmd_line, | |
| 95 FilePath* mini_installer) { | |
|
robertshield
2011/01/28 18:36:22
DCHECK(mini_installer)
grt (UTC plus 2)
2011/01/28 19:13:33
Done.
| |
| 96 FilePath result = cmd_line.GetSwitchValuePath(switches::kMiniInstaller); | |
| 97 if (result.empty() && PathService::Get(base::DIR_EXE, &result)) | |
| 98 result = result.Append(kMiniInstallerExe); | |
| 99 if (result.empty()) | |
| 100 return false; | |
| 101 *mini_installer = result; | |
| 102 return true; | |
| 103 } | |
| 104 | |
| 105 // Gets the path to the output file, putting the result in |out|. | |
| 106 void GetOutPath(const CommandLine& cmd_line, FilePath* out) { | |
|
robertshield
2011/01/28 18:36:22
DCHECK(out)
grt (UTC plus 2)
2011/01/28 19:13:33
Done.
| |
| 107 FilePath result = cmd_line.GetSwitchValuePath(switches::kOut); | |
| 108 if (result.empty()) | |
| 109 *out = FilePath(kMiniInstallerNewExe); | |
| 110 else | |
| 111 *out = result; | |
| 112 } | |
| 113 | |
| 114 // Returns the direction in which the version should be adjusted. | |
| 115 upgrade_test::Direction GetDirection(const CommandLine& cmd_line) { | |
| 116 return cmd_line.HasSwitch(switches::kPrevious) ? | |
| 117 upgrade_test::PREVIOUS_VERSION : upgrade_test::NEXT_VERSION; | |
| 118 } | |
| 119 | |
| 120 } // namespace | |
| 121 | |
| 122 // The main program. | |
| 123 int wmain(int argc, wchar_t *argv[]) { | |
| 124 base::AtExitManager exit_manager; | |
| 125 CommandLine::Init(0, NULL); | |
| 126 CommandLine* cmd_line = CommandLine::ForCurrentProcess(); | |
| 127 | |
| 128 if (cmd_line->HasSwitch(switches::kHelp)) { | |
| 129 DumpUsage(*cmd_line, errors::SHOW_HELP, std::wstring()); | |
| 130 return EXIT_SUCCESS; | |
| 131 } | |
| 132 | |
| 133 FilePath mini_installer; | |
| 134 if (!GetMiniInstallerPath(*cmd_line, &mini_installer)) { | |
| 135 DumpUsage(*cmd_line, errors::MINI_INSTALLER_NOT_FOUND, std::wstring()); | |
| 136 return EXIT_FAILURE; | |
| 137 } | |
| 138 | |
| 139 if (!file_util::PathExists(mini_installer)) { | |
| 140 DumpUsage(*cmd_line, errors::MINI_INSTALLER_NOT_FOUND, | |
| 141 mini_installer.value()); | |
| 142 return EXIT_FAILURE; | |
| 143 } | |
| 144 | |
| 145 FilePath out; | |
| 146 GetOutPath(*cmd_line, &out); | |
| 147 if (file_util::PathExists(out)) { | |
| 148 DumpUsage(*cmd_line, errors::OUT_FILE_EXISTS, out.value()); | |
| 149 return EXIT_FAILURE; | |
| 150 } | |
| 151 | |
| 152 upgrade_test::Direction direction = GetDirection(*cmd_line); | |
| 153 | |
| 154 std::wstring original_version; | |
| 155 std::wstring new_version; | |
| 156 | |
| 157 if (upgrade_test::GenerateAlternateVersion(mini_installer, out, direction, | |
| 158 &original_version, &new_version)) { | |
| 159 fwprintf(stdout, L"Generated version %s from version %s\n", | |
| 160 new_version.c_str(), original_version.c_str()); | |
| 161 return EXIT_SUCCESS; | |
| 162 } | |
| 163 | |
| 164 DumpUsage(*cmd_line, errors::GENERATION_FAILED, mini_installer.value()); | |
| 165 return EXIT_FAILURE; | |
| 166 } | |
| OLD | NEW |