Chromium Code Reviews| Index: chrome/installer/test/alternate_version_generator_main.cc |
| =================================================================== |
| --- chrome/installer/test/alternate_version_generator_main.cc (revision 0) |
| +++ chrome/installer/test/alternate_version_generator_main.cc (revision 0) |
| @@ -0,0 +1,166 @@ |
| +// Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +// This file provides a command-line interface to |
| +// upgrade_test::GenerateAlternateVersion(). |
| + |
| +#include <cstdio> |
| +#include <cstdlib> |
| + |
| +#include "base/at_exit.h" |
| +#include "base/basictypes.h" |
| +#include "base/command_line.h" |
| +#include "base/file_path.h" |
| +#include "base/file_util.h" |
| +#include "base/logging.h" |
| +#include "base/path_service.h" |
| +#include "chrome/installer/test/alternate_version_generator.h" |
| + |
| +namespace { |
| + |
| +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.
|
| +const wchar_t kMiniInstallerNewExe[] = L"mini_installer_new.exe"; |
| + |
| +namespace switches { |
| + |
| +const char kHelp[] = "help"; |
| +const char kMiniInstaller[] = "mini_installer"; |
| +const char kOut[] = "out"; |
| +const char kPrevious[] = "previous"; |
| + |
| +} // namespace switches |
| + |
| +namespace errors { |
| + |
| +enum ErrorCode { |
| + SHOW_HELP, |
| + MINI_INSTALLER_NOT_FOUND, |
| + OUT_FILE_EXISTS, |
| + GENERATION_FAILED |
| +}; |
| + |
| +const wchar_t* const Messages[] = { |
| + NULL, |
| + L"original mini_installer.exe not found", |
| + L"output file already exists", |
| + L"failed to generate a newly versioned mini_installer.exe" |
| +}; |
| + |
| +const wchar_t* GetErrorMessage(ErrorCode error_code) { |
| + DCHECK_LE(0, error_code); |
| + DCHECK_GT(arraysize(Messages), static_cast<size_t>(error_code)); |
| + return Messages[error_code]; |
| +} |
| + |
| +} // namespace errors |
| + |
| +// Display usage information to stderr along with an optional error message with |
| +// details. |
| +void DumpUsage(const CommandLine& cmd_line, |
| + errors::ErrorCode error_code, |
| + const std::wstring& detail) { |
| + const wchar_t* error_message = errors::GetErrorMessage(error_code); |
| + if (error_message != NULL) { |
| + fwprintf(stderr, L"%s: %s", cmd_line.GetProgram().value().c_str(), |
| + errors::GetErrorMessage(error_code)); |
| + if (!detail.empty()) |
| + fwprintf(stderr, L" (%s)\n", detail.c_str()); |
| + else |
| + fwprintf(stderr, L"\n"); |
| + } |
| + |
| + fwprintf(stderr, |
| +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.
|
| +L" [ --7za_path=7ZA_PATH ]\n" |
| +L" --help Display this help message.\n" |
| +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.
|
| +L" Default value is \"mini_installer.exe\" in the same\n" // NOLINT |
| +L" directory as this program.\n" |
| +L" --out=OUT_PATH Relative or absolute path to output file. Default\n" // NOLINT |
| +L" value is \"mini_installer_new.exe\" in the current\n" // NOLINT |
| +L" directory.\n" |
| +L" --previous OUT_PATH will have a lower version than SRC_PATH.\n" // NOLINT |
| +L" By default, OUT_PATH will have a higher version.\n" // NOLINT |
| +L" --7za_path=7ZA_PATH Relative or absolute path to the directory holding\n" // NOLINT |
| +L" 7za.exe. Default value is\n" |
| +L" ..\\..\\third_party\\lzma_sdk\\Executable relative to\n" // NOLINT |
| +L" the directory holding this program.", |
| + cmd_line.GetProgram().value().c_str()); |
| +} |
| + |
| +// Gets the path to the source mini_installer.exe on which to operate, putting |
| +// the result in |mini_installer|. Returns true on success. |
| +bool GetMiniInstallerPath(const CommandLine& cmd_line, |
| + FilePath* mini_installer) { |
|
robertshield
2011/01/28 18:36:22
DCHECK(mini_installer)
grt (UTC plus 2)
2011/01/28 19:13:33
Done.
|
| + FilePath result = cmd_line.GetSwitchValuePath(switches::kMiniInstaller); |
| + if (result.empty() && PathService::Get(base::DIR_EXE, &result)) |
| + result = result.Append(kMiniInstallerExe); |
| + if (result.empty()) |
| + return false; |
| + *mini_installer = result; |
| + return true; |
| +} |
| + |
| +// Gets the path to the output file, putting the result in |out|. |
| +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.
|
| + FilePath result = cmd_line.GetSwitchValuePath(switches::kOut); |
| + if (result.empty()) |
| + *out = FilePath(kMiniInstallerNewExe); |
| + else |
| + *out = result; |
| +} |
| + |
| +// Returns the direction in which the version should be adjusted. |
| +upgrade_test::Direction GetDirection(const CommandLine& cmd_line) { |
| + return cmd_line.HasSwitch(switches::kPrevious) ? |
| + upgrade_test::PREVIOUS_VERSION : upgrade_test::NEXT_VERSION; |
| +} |
| + |
| +} // namespace |
| + |
| +// The main program. |
| +int wmain(int argc, wchar_t *argv[]) { |
| + base::AtExitManager exit_manager; |
| + CommandLine::Init(0, NULL); |
| + CommandLine* cmd_line = CommandLine::ForCurrentProcess(); |
| + |
| + if (cmd_line->HasSwitch(switches::kHelp)) { |
| + DumpUsage(*cmd_line, errors::SHOW_HELP, std::wstring()); |
| + return EXIT_SUCCESS; |
| + } |
| + |
| + FilePath mini_installer; |
| + if (!GetMiniInstallerPath(*cmd_line, &mini_installer)) { |
| + DumpUsage(*cmd_line, errors::MINI_INSTALLER_NOT_FOUND, std::wstring()); |
| + return EXIT_FAILURE; |
| + } |
| + |
| + if (!file_util::PathExists(mini_installer)) { |
| + DumpUsage(*cmd_line, errors::MINI_INSTALLER_NOT_FOUND, |
| + mini_installer.value()); |
| + return EXIT_FAILURE; |
| + } |
| + |
| + FilePath out; |
| + GetOutPath(*cmd_line, &out); |
| + if (file_util::PathExists(out)) { |
| + DumpUsage(*cmd_line, errors::OUT_FILE_EXISTS, out.value()); |
| + return EXIT_FAILURE; |
| + } |
| + |
| + upgrade_test::Direction direction = GetDirection(*cmd_line); |
| + |
| + std::wstring original_version; |
| + std::wstring new_version; |
| + |
| + if (upgrade_test::GenerateAlternateVersion(mini_installer, out, direction, |
| + &original_version, &new_version)) { |
| + fwprintf(stdout, L"Generated version %s from version %s\n", |
| + new_version.c_str(), original_version.c_str()); |
| + return EXIT_SUCCESS; |
| + } |
| + |
| + DumpUsage(*cmd_line, errors::GENERATION_FAILED, mini_installer.value()); |
| + return EXIT_FAILURE; |
| +} |
| Property changes on: chrome\installer\test\alternate_version_generator_main.cc |
| ___________________________________________________________________ |
| Added: svn:eol-style |
| + LF |