| 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 kDefaultMiniInstallerFile[] = L"mini_installer.exe"; |
| 23 const wchar_t kDefaultOutPath[] = 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 [ OPTIONS ]\n" |
| 75 L" Where OPTIONS is one or more of:\n" |
| 76 L" --help Display this help message.\n" |
| 77 L" --mini_installer=SRC_PATH Path to mini_installer.exe. Default value is\n" |
| 78 L" \"mini_installer.exe\" in the same directory as\n" |
| 79 L" this program.\n" |
| 80 L" --out=OUT_PATH Path to output file. Default value is\n" |
| 81 L" \"mini_installer_new.exe\" in the current\n" |
| 82 L" directory.\n" |
| 83 L" --previous OUT_PATH will have a lower version than\n" |
| 84 L" SRC_PATH. By default, OUT_PATH will have a\n" |
| 85 L" higher version.\n" |
| 86 L" --7za_path=7ZA_PATH Path to the directory holding 7za.exe. Defaults\n" |
| 87 L" to ..\\..\\third_party\\lzma_sdk\\Executable\n" |
| 88 L" relative to this program's location.\n", |
| 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) { |
| 96 DCHECK(mini_installer); |
| 97 FilePath result = cmd_line.GetSwitchValuePath(switches::kMiniInstaller); |
| 98 if (result.empty() && PathService::Get(base::DIR_EXE, &result)) |
| 99 result = result.Append(kDefaultMiniInstallerFile); |
| 100 if (result.empty()) |
| 101 return false; |
| 102 *mini_installer = result; |
| 103 return true; |
| 104 } |
| 105 |
| 106 // Gets the path to the output file, putting the result in |out|. |
| 107 void GetOutPath(const CommandLine& cmd_line, FilePath* out) { |
| 108 DCHECK(out); |
| 109 FilePath result = cmd_line.GetSwitchValuePath(switches::kOut); |
| 110 if (result.empty()) |
| 111 *out = FilePath(kDefaultOutPath); |
| 112 else |
| 113 *out = result; |
| 114 } |
| 115 |
| 116 // Returns the direction in which the version should be adjusted. |
| 117 upgrade_test::Direction GetDirection(const CommandLine& cmd_line) { |
| 118 return cmd_line.HasSwitch(switches::kPrevious) ? |
| 119 upgrade_test::PREVIOUS_VERSION : upgrade_test::NEXT_VERSION; |
| 120 } |
| 121 |
| 122 } // namespace |
| 123 |
| 124 // The main program. |
| 125 int wmain(int argc, wchar_t *argv[]) { |
| 126 base::AtExitManager exit_manager; |
| 127 CommandLine::Init(0, NULL); |
| 128 CommandLine* cmd_line = CommandLine::ForCurrentProcess(); |
| 129 |
| 130 if (cmd_line->HasSwitch(switches::kHelp)) { |
| 131 DumpUsage(*cmd_line, errors::SHOW_HELP, std::wstring()); |
| 132 return EXIT_SUCCESS; |
| 133 } |
| 134 |
| 135 FilePath mini_installer; |
| 136 if (!GetMiniInstallerPath(*cmd_line, &mini_installer)) { |
| 137 DumpUsage(*cmd_line, errors::MINI_INSTALLER_NOT_FOUND, std::wstring()); |
| 138 return EXIT_FAILURE; |
| 139 } |
| 140 |
| 141 if (!file_util::PathExists(mini_installer)) { |
| 142 DumpUsage(*cmd_line, errors::MINI_INSTALLER_NOT_FOUND, |
| 143 mini_installer.value()); |
| 144 return EXIT_FAILURE; |
| 145 } |
| 146 |
| 147 FilePath out; |
| 148 GetOutPath(*cmd_line, &out); |
| 149 if (file_util::PathExists(out)) { |
| 150 DumpUsage(*cmd_line, errors::OUT_FILE_EXISTS, out.value()); |
| 151 return EXIT_FAILURE; |
| 152 } |
| 153 |
| 154 upgrade_test::Direction direction = GetDirection(*cmd_line); |
| 155 |
| 156 std::wstring original_version; |
| 157 std::wstring new_version; |
| 158 |
| 159 if (upgrade_test::GenerateAlternateVersion(mini_installer, out, direction, |
| 160 &original_version, &new_version)) { |
| 161 fwprintf(stdout, L"Generated version %s from version %s\n", |
| 162 new_version.c_str(), original_version.c_str()); |
| 163 return EXIT_SUCCESS; |
| 164 } |
| 165 |
| 166 DumpUsage(*cmd_line, errors::GENERATION_FAILED, mini_installer.value()); |
| 167 return EXIT_FAILURE; |
| 168 } |
| OLD | NEW |