-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
consolidated changes from other branches here, cleanly (#246)
- Loading branch information
Showing
78 changed files
with
1,341 additions
and
918 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
#include <iostream> | ||
#include <fstream> | ||
#include <cstdlib> | ||
#include <cstring> | ||
#include <sys/stat.h> | ||
#include <sys/types.h> | ||
#include <vector> | ||
#include <algorithm> | ||
#include <istream> | ||
#include <string> | ||
#include <windows.h> | ||
|
||
using namespace std; | ||
|
||
constexpr char* pathSeparator = ";"; | ||
constexpr char* dirSeparator = "\\"; | ||
constexpr char* azdBinaryFileName = "azd.exe"; | ||
constexpr char* azdFullNameCacheFile = "azd.ini"; | ||
constexpr char* azdFullNameMustContains = "Azure Dev CLI"; | ||
|
||
constexpr char* aiCommandName = "ai"; | ||
constexpr char* aiBinaryFileName = "ai.exe"; | ||
|
||
string findMyself() { | ||
char path[MAX_PATH]; | ||
GetModuleFileNameA(NULL, path, MAX_PATH); | ||
|
||
auto whereThisBinaryLives = string(path); | ||
auto lastSlash = whereThisBinaryLives.find_last_of(dirSeparator); | ||
if (lastSlash != string::npos) { | ||
whereThisBinaryLives = whereThisBinaryLives.substr(0, lastSlash); | ||
} | ||
else { | ||
whereThisBinaryLives = "."; | ||
} | ||
|
||
return whereThisBinaryLives; | ||
} | ||
|
||
bool fileExists(const string& name) { | ||
struct stat buffer; | ||
return (stat (name.c_str(), &buffer) == 0); | ||
} | ||
|
||
string findAzdBinaryInPath() { | ||
vector<string> paths; | ||
char* path = getenv("PATH"); | ||
char* token = strtok(path, pathSeparator); | ||
while (token != NULL) { | ||
paths.push_back(string(token)); | ||
token = strtok(NULL, pathSeparator); | ||
} | ||
|
||
for (auto& p : paths) { | ||
string withPath = p + std::string(dirSeparator) + azdBinaryFileName; | ||
if (fileExists(withPath) && withPath.find(azdFullNameMustContains) != string::npos) { | ||
return withPath; | ||
} | ||
} | ||
|
||
printf("ERROR: `%s` not found in PATH!!\n\n TRY: Update PATH to include location %s...\n OR: Update %s with full path to %s", azdBinaryFileName, azdBinaryFileName, azdFullNameCacheFile, azdBinaryFileName); | ||
exit(1); | ||
} | ||
|
||
string findAndCacheAzdBinary() | ||
{ | ||
auto whereThisBinaryLives = findMyself(); | ||
|
||
auto sideBySideCacheFile = whereThisBinaryLives + dirSeparator + azdFullNameCacheFile; | ||
ifstream cacheStream(sideBySideCacheFile); | ||
|
||
string cachedContent; | ||
if (cacheStream.good()) { | ||
getline(cacheStream, cachedContent); | ||
cacheStream.close(); | ||
} else { | ||
cachedContent = findAzdBinaryInPath(); | ||
std::ofstream outFile(sideBySideCacheFile); | ||
outFile << cachedContent; | ||
outFile.close(); | ||
} | ||
|
||
return cachedContent; | ||
} | ||
|
||
string quoteArgIfNeeded(const char* arg) { | ||
|
||
bool needsQuoted = false; | ||
string argStr(arg); | ||
|
||
if (argStr.find('\"') != string::npos) { | ||
needsQuoted = true; | ||
size_t pos = 0; | ||
while ((pos = argStr.find("\"", pos)) != string::npos) { | ||
argStr.replace(pos, 1, "\\\""); | ||
pos += 2; | ||
} | ||
} | ||
|
||
if (argStr.find(' ') != string::npos) { | ||
needsQuoted = true; | ||
} | ||
|
||
return needsQuoted | ||
? "\"" + argStr + "\"" | ||
: argStr; | ||
} | ||
|
||
int main(int argc, char* argv[]) { | ||
|
||
auto actualBinaryFullFileName = findAndCacheAzdBinary(); | ||
|
||
if (argc > 1 && string(argv[1]) == aiCommandName) { | ||
string commandLine(aiBinaryFileName); | ||
for (int i = 2; i < argc; i++) { | ||
commandLine += " "; | ||
commandLine += quoteArgIfNeeded(argv[i]); | ||
} | ||
return system(commandLine.c_str()); | ||
} else { | ||
string commandLine("\"" + actualBinaryFullFileName + "\""); | ||
for (int i = 1; i < argc; i++) { | ||
commandLine += " "; | ||
commandLine += quoteArgIfNeeded(argv[i]); | ||
} | ||
return system(commandLine.c_str()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
___ ____ ___ _____ | ||
/ _ /_ / / _ |/_ _/ | ||
/ __ |/ /_/ __ |_/ /_ | ||
/_/ |_/___/_/ |_/____/ | ||
___ _____ | ||
/ _ |/_ _/ | ||
/ __ |_/ /_ | ||
/_/ |_/____/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
___ ____ ___ _____ ___ _____ __ | ||
/ _ /_ / / _ |/_ _/ / _ \/ __/ | / / | ||
/ __ |/ /_/ __ |_/ /_ / // / _/ | |/ / | ||
/_/ |_/___/_/ |_/____/ /____/___/ |___/ | ||
___ _____ ___ _____ __ | ||
/ _ |/_ _/ / _ \/ __/ | / / | ||
/ __ |_/ /_ / // / _/ | |/ / | ||
/_/ |_/____/ /____/___/ |___/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
___ ____ ___ _____ ___ _____ __ ___ __ _ ___ __ __ | ||
/ _ /_ / / _ |/_ _/ / _ \/ __/ | / / / __/ // / __/ / / / | ||
/ __ |/ /_/ __ |_/ /_ / // / _/ | |/ / _\ \/ _ / _// /__/ /__ | ||
/_/ |_/___/_/ |_/____/ /____/___/ |___/ /___/_//_/___/____/____/ | ||
___ _____ ___ _____ __ ___ __ _ ___ __ __ | ||
/ _ |/_ _/ / _ \/ __/ | / / / __/ // / __/ / / / | ||
/ __ |_/ /_ / // / _/ | |/ / _\ \/ _ / _// /__/ /__ | ||
/_/ |_/____/ /____/___/ |___/ /___/_//_/___/____/____/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
___ ____ ___ _____ __ __ __ _ _______ | ||
/ _ /_ / / _ |/_ _/ / / |/ / /__ __/ | ||
/ __ |/ /_/ __ |_/ /_ / / /| / / / / | ||
/_/ |_/___/_/ |_/____/ /__/_/ |_/__/ /_/ | ||
___ _____ __ __ __ _ _______ | ||
/ _ |/_ _/ / / |/ / /__ __/ | ||
/ __ |_/ /_ / / /| / / / / | ||
/_/ |_/____/ /__/_/ |_/__/ /_/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
___ ____ ___ _____ _ _ _____ ___ ___ ___ ___ | ||
/ _ /_ / / _ |/_ _/ | | /| / /_ _/_ / / _ | / _ \/ _ \ | ||
/ __ |/ /_/ __ |_/ /_ | |/ |/ /_/ /_ / /_/ __ |/ , _/ // / | ||
/_/ |_/___/_/ |_/____/ |__/|__/_____//___/_/ |_/_/|_/____/ | ||
___ _____ _ _ _____ ___ ___ ___ ___ | ||
/ _ |/_ _/ | | /| / /_ _/_ / / _ | / _ \/ _ \ | ||
/ __ |_/ /_ | |/ |/ /_/ /_ / /_/ __ |/ , _/ // / | ||
/_/ |_/____/ |__/|__/_____//___/_/ |_/_/|_/____/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.