Skip to content
This repository was archived by the owner on Jan 6, 2021. It is now read-only.

Commit 5ca0485

Browse files
committed
コマンドファイルをリソースにするのをやめる
1 parent fd94b56 commit 5ca0485

7 files changed

+20
-68
lines changed

include/BatchCommand.h

-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ class BatchCommand {
99
typedef BatchCommand _Myt;
1010

1111
public:
12-
BatchCommand(_In_ WORD wBatchResourceId);
1312
BatchCommand(_In_opt_ const std::wstring& batchContent = L"");
1413
BatchCommand(_Inout_ _Myt &&other) = delete;
1514
_Myt& operator = (_Inout_ _Myt &&rhs) = delete;

include/GitHubPR.h

+1-2
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,9 @@ class GitHubPrApp {
1313
PrivateProfile profile;
1414
std::wstring profileBuffer;
1515
std::wstring gitRemoteName;
16-
std::wstring homeBranch;
1716
std::wstring branchPrefix;
1817
std::wstring prNumber;
19-
std::wstring currentBranch;
18+
bool branchNameExists;
2019

2120
public:
2221
void initSettings(_In_ const std::wstring &targetDir);

include/resource.h

-4
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,6 @@
22
// Microsoft Visual C++ generated include file.
33
// Used by Win32Project1.rc
44
//
5-
#define IDR_CMD_GET_PR 101
6-
#define IDR_CMD_ENUM_REMOTES 102
7-
#define IDR_CMD_ENUM_BRANCHS 103
8-
#define IDR_CMD_CHECKOUT_BRANCH 104
95
#define IDS_ENTER_PRNUMBER 105
106
#define IDS_ENTER_BRANCH 106
117
#define IDS_ENTER_REMOTE 107

src/BatchCommand.cpp

-28
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,6 @@
99
#endif /* __MINGW32__ */
1010

1111

12-
BatchCommand::BatchCommand(_In_ WORD wBatchResourceId)
13-
{
14-
batContent = loadBatchContent(wBatchResourceId);
15-
batName = generateBatchName();
16-
}
17-
1812
BatchCommand::BatchCommand(_In_opt_ const std::wstring& batchContent)
1913
{
2014
batContent = std::move(batchContent);
@@ -33,28 +27,6 @@ std::wstring BatchCommand::generateBatchName()
3327
return std::wstring(fnTemplate);
3428
}
3529

36-
// リソースからバッチ内容を読み込む
37-
std::wstring BatchCommand::loadBatchContent(_In_ WORD wBatchResourceId)
38-
{
39-
HRSRC hres = ::FindResource(NULL, MAKEINTRESOURCE(wBatchResourceId), (LPCWSTR)L"BatchCommands");
40-
if (!hres) {
41-
THROW_APP_EXCEPTION("resource not found.");
42-
}
43-
const SIZE_T cbResData = ::SizeofResource(NULL, hres);
44-
HGLOBAL hResData = ::LoadResource(NULL, hres);
45-
if (!hResData) {
46-
THROW_APP_EXCEPTION("load resource failed.");
47-
}
48-
LPCSTR pResData = static_cast<LPSTR>(::LockResource(hResData));
49-
if (!pResData) {
50-
THROW_APP_EXCEPTION("lock resource failed.");
51-
}
52-
std::wstring batchContent(convertMbsToWString(pResData, cbResData));
53-
UnlockResource(hResData);
54-
55-
return std::move(batchContent);
56-
}
57-
5830
//バッチを実行してストリームを取得する
5931
std::unique_ptr<std::wistream> BatchCommand::invokeAndGetStream()
6032
{

src/GitHubPR.cpp

+19-11
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ const WCHAR PR_NUMBER[] = L"PR_NUMBER";
2222

2323
void GitHubPrApp::initSettings(_In_ const std::wstring &targetDir)
2424
{
25+
constexpr bool saveEnvStr = false;
2526
constexpr bool skipSave = true;
2627

2728
::SetConsoleTitle(GITHUB_PR);
@@ -55,20 +56,17 @@ void GitHubPrApp::initSettings(_In_ const std::wstring &targetDir)
5556
std::wstring profileName = curDir + PROFILE_SUFFIX;
5657
profile = std::move(PrivateProfile(profileName));
5758

58-
gitRemoteName = _initSetting(GIT_REMOTE_NAME, [this]() {return _getRemoteName(); }, skipSave);
59+
gitRemoteName = _initSetting(GIT_REMOTE_NAME, [this]() {return _getRemoteName(); }, saveEnvStr);
5960
branchPrefix = _initSetting(BRANCH_PREFIX, [this]() {return _getBranchPrefix(); }, skipSave);
6061
prNumber = _initSetting(PR_NUMBER, [this]() {return _getPrNumber(); }, skipSave);
6162

6263
{
63-
BatchCommand cmd(IDR_CMD_CHECKOUT_BRANCH);
64+
BatchCommand cmd(L"@%GIT% checkout %BRANCH_NAME% > NUL 2>&1\r\n@ECHO %ERRORLEVEL%");
6465
auto retList = cmd.invokeAndGetLines();
65-
if (!retList.empty()) {
66-
currentBranch = retList.back();
66+
if (retList.empty()) {
67+
THROW_APP_EXCEPTION("git checkout may not work correctly.");
6768
}
68-
}
69-
if (!currentBranch.empty()) {
70-
setEnvStr(L"CURRENT_BRANCH", currentBranch.c_str());
71-
homeBranch = _initSetting(HOME_BRANCH, [this]() {return _getHomeBranch(); }, skipSave);
69+
branchNameExists = retList.front() == L"0";
7270
}
7371
}
7472

@@ -114,7 +112,7 @@ std::wstring GitHubPrApp::_getRemoteName() const
114112
{
115113
std::list<std::wstring> remotes;
116114
{
117-
BatchCommand cmd(IDR_CMD_ENUM_REMOTES);
115+
BatchCommand cmd(L"@%GIT% remote");
118116
remotes = cmd.invokeAndGetLines();
119117
remotes.remove(L"");
120118
}
@@ -174,7 +172,7 @@ std::wstring GitHubPrApp::_getHomeBranch() const
174172
{
175173
std::list<std::wstring> branchs;
176174
{
177-
BatchCommand cmd(IDR_CMD_ENUM_BRANCHS);
175+
BatchCommand cmd(L"@%GIT% branch --list");
178176
branchs = cmd.invokeAndGetLines();
179177
branchs.remove(L"");
180178
std::wregex reBranch(L"^\\*?\\s+(.+)$");
@@ -218,8 +216,18 @@ std::wstring GitHubPrApp::_getHomeBranch() const
218216

219217
void GitHubPrApp::executeBatch() const
220218
{
219+
//実行するコマンドを組み立てる
220+
std::wostringstream os;
221+
if (!branchNameExists) {
222+
os << L"@%GIT% fetch %GIT_REMOTE_NAME% pull/%PR_NUMBER%/head:%BRANCH_NAME%" << std::endl;
223+
os << L"@%GIT% checkout %BRANCH_NAME%" << std::endl;
224+
}
225+
else {
226+
os << L"@%GIT% pull %GIT_REMOTE_NAME% pull/%PR_NUMBER%/head" << std::endl;
227+
}
228+
221229
//バッチファイルを実行する
222-
BatchCommand cmd(IDR_CMD_GET_PR);
230+
BatchCommand cmd(os.str());
223231
auto lines(cmd.invokeAndGetLines());
224232
std::for_each(lines.cbegin(), lines.cend(), [](const std::wstring &line) {std::wcout << line << std::endl; });
225233
}

src/GitHubPR.rc

-10
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,6 @@
1919
LANGUAGE LANG_NEUTRAL, SUBLANG_NEUTRAL
2020
#pragma code_page(65001)
2121

22-
/////////////////////////////////////////////////////////////////////////////
23-
//
24-
// BatchCommands
25-
//
26-
27-
IDR_CMD_GET_PR BatchCommands "../res/bat_get_pr.cmd"
28-
IDR_CMD_ENUM_REMOTES BatchCommands "../res/bat_enum_remotes.cmd"
29-
IDR_CMD_ENUM_BRANCHS BatchCommands "../res/bat_enum_branchs.cmd"
30-
IDR_CMD_CHECKOUT_BRANCH BatchCommands "../res/bat_checkout_branch.cmd"
31-
3222
/////////////////////////////////////////////////////////////////////////////
3323
//
3424
// Icon

test/BatchCommandTest.cpp

-12
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,6 @@ TEST(BatchCommandTest, constructWithNoArg)
1111
ASSERT_TRUE(cmd.GetContent().empty());
1212
}
1313

14-
TEST(BatchCommandTest, constructWithResource)
15-
{
16-
BatchCommand cmd(IDR_CMD_CHECKOUT_BRANCH);
17-
EXPECT_FALSE(cmd.GetName().empty());
18-
EXPECT_FALSE(cmd.GetContent().empty());
19-
}
20-
2114
TEST(BatchCommandTest, constructWithCmdEcho)
2215
{
2316
const WCHAR content[] = L"@echo test";
@@ -27,11 +20,6 @@ TEST(BatchCommandTest, constructWithCmdEcho)
2720
ASSERT_STREQ(content, cmd.GetContent().c_str());
2821
}
2922

30-
TEST(BatchCommandTest, constructWithInvalidResource)
31-
{
32-
ASSERT_THROW({ BatchCommand cmd((WORD)0xFFFFu); }, std::runtime_error);
33-
}
34-
3523
TEST(BatchCommandTest, executeBatchWithoutContent)
3624
{
3725
BatchCommand cmd;

0 commit comments

Comments
 (0)