diff options
| author | Leitwolf11 | 2026-11-05 06:29:20 +0300 |
|---|---|---|
| committer | Leitwolf11 | 2026-11-05 06:29:20 +0300 |
| commit | 52490ff852f3d72ce9c9eb76898080223e9e17fd (patch) | |
| tree | 4dd64d7cdce703fa15956dfa51bf587b3f397153 /Sources | |
| download | tazelish-52490ff852f3d72ce9c9eb76898080223e9e17fd.tar.gz | |
Start Tazelish with its command line and version files
Tazelish installs Tazel and chooses which version of it runs, for those
who use Tazel alone. This first part holds everything that needs no
download:
- Versions: MAJOR.SEMIMAJOR.MINOR.PATCH without leading zeros,
unstable, nightly, and YYYY-MM-DD-unstable or -nightly with a date
the calendar has.
- The command line, read by hand into a typed command: install, use
and default with --force before or after the version, self
update|remove|purge, and help with or without a command. A wrong
command line ends with exit code 2.
- tazelish.txt and config.txt: the nearest tazelish.txt in the current
folder or above it comes first, then $HOME/.tazelish/config.txt or
%USERPROFILE%\.tazelish\config.txt. use and default refuse to write
over a file that exists unless --force is given.
install and self download from static.tazel.net, which this build does
not do; they say so and end with exit code 1.
C++17 with fmt, tested with Catch3, built by Bazel (clang-cl on
Windows, with the rules_cc patch the Tala repository carries). The code
throws no exceptions and builds with warnings as errors; the tests
leave out -Wconversion, which the headers of Catch3 do not pass. CI
tests on Linux, macOS and Windows.
Diffstat (limited to 'Sources')
| -rw-r--r-- | Sources/CommandLine.cpp | 218 | ||||
| -rw-r--r-- | Sources/Help.cpp | 105 | ||||
| -rw-r--r-- | Sources/Main.cpp | 140 | ||||
| -rw-r--r-- | Sources/Version.cpp | 196 | ||||
| -rw-r--r-- | Sources/VersionFiles.cpp | 195 |
5 files changed, 854 insertions, 0 deletions
diff --git a/Sources/CommandLine.cpp b/Sources/CommandLine.cpp new file mode 100644 index 0000000..67288d9 --- /dev/null +++ b/Sources/CommandLine.cpp @@ -0,0 +1,218 @@ +// SPDX-FileCopyrightText: 2026 Progmasoft <support@progmasoft.com> +// SPDX-License-Identifier: MPL-2.0 WITH AdditionRef-Progmasoft-Exception-2.0 + +#include <array> +#include <fmt/format.h> +#include <utility> + +#include "Progmasoft/Tazelish/CommandLine.hpp" + +namespace Progmasoft::Tazelish +{ + namespace + { + + constexpr std::array<std::pair<std::string_view, Topic>, 5> kTopics + = { { + { "install", Topic::kInstall }, + { "use", Topic::kUse }, + { "default", Topic::kDefault }, + { "self", Topic::kSelf }, + { "help", Topic::kHelp }, + } }; + + constexpr std::string_view kVersionForms + = "MAJOR.SEMIMAJOR.MINOR.PATCH, unstable, nightly, " + "YYYY-MM-DD-unstable or " + "YYYY-MM-DD-nightly"; + + Rejection + Reject(std::string message) + { + return Rejection{ std::move(message) }; + } + + // The one version of a command, written as the argument [text]. + std::variant<Version, Rejection> + ReadVersion(std::string_view command, std::string_view text) + { + if (auto version = ParseVersion(text)) + { + return *version; + } + return Reject( + fmt::format("'{}' is not a version; tazelish {} takes {}", + text, + command, + kVersionForms)); + } + + // `install <version>`. + ParseResult + ParseInstall(const std::vector<std::string_view> &rest) + { + if (rest.size() != 1) + { + return Reject("tazelish install takes one version, such as " + "tazelish install 0.4.0.3"); + } + auto version = ReadVersion("install", rest.front()); + if (auto *rejection = std::get_if<Rejection>(&version)) + { + return std::move(*rejection); + } + return Command{ Install{ std::get<Version>(version) } }; + } + + // `use|default [--force] <version>`, the flag before or after the + // version. + template<typename Writing> + ParseResult + ParseWriting(std::string_view command, + const std::vector<std::string_view> &rest) + { + bool force = false; + std::optional<std::string_view> text; + for (const std::string_view argument : rest) + { + if (argument == "--force") + { + if (force) + { + return Reject(fmt::format( + "--force is written twice in tazelish {}", + command)); + } + force = true; + } + else if (argument.size() > 1 && argument.front() == '-') + { + return Reject(fmt::format("'{}' is not an option of " + "tazelish {}; it takes --force", + argument, + command)); + } + else if (text) + { + return Reject( + fmt::format("tazelish {} takes one version", command)); + } + else + { + text = argument; + } + } + if (!text) + { + return Reject(fmt::format( + "tazelish {} takes a version, such as tazelish {} 0.4.0.3", + command, + command)); + } + auto version = ReadVersion(command, *text); + if (auto *rejection = std::get_if<Rejection>(&version)) + { + return std::move(*rejection); + } + return Command{ Writing{ std::get<Version>(version), force } }; + } + + // `self update|remove|purge`. + ParseResult + ParseSelf(const std::vector<std::string_view> &rest) + { + if (rest.size() == 1) + { + if (rest.front() == "update") + { + return Command{ Self{ SelfAction::kUpdate } }; + } + if (rest.front() == "remove") + { + return Command{ Self{ SelfAction::kRemove } }; + } + if (rest.front() == "purge") + { + return Command{ Self{ SelfAction::kPurge } }; + } + } + return Reject("tazelish self takes one of update, remove or purge"); + } + + // `help [command]`. + ParseResult + ParseHelp(const std::vector<std::string_view> &rest) + { + if (rest.empty()) + { + return Command{ Help{} }; + } + if (rest.size() == 1) + { + for (const auto &[name, topic] : kTopics) + { + if (rest.front() == name) + { + return Command{ Help{ topic } }; + } + } + return Reject( + fmt::format("'{}' is not a command of tazelish; write " + "tazelish help for the commands", + rest.front())); + } + return Reject("tazelish help takes at most one command"); + } + + } // namespace + + ParseResult + ParseCommandLine(const std::vector<std::string_view> &arguments) + { + if (arguments.empty()) + { + return Command{ Help{} }; + } + const std::string_view command = arguments.front(); + const std::vector<std::string_view> rest(arguments.begin() + 1, + arguments.end()); + if (command == "install") + { + return ParseInstall(rest); + } + if (command == "use") + { + return ParseWriting<Use>(command, rest); + } + if (command == "default") + { + return ParseWriting<Default>(command, rest); + } + if (command == "self") + { + return ParseSelf(rest); + } + if (command == "help") + { + return ParseHelp(rest); + } + return Reject(fmt::format( + "'{}' is not a command of tazelish; write tazelish help for the " + "commands", + command)); + } + + std::string_view + TopicName(Topic topic) + { + for (const auto &[name, candidate] : kTopics) + { + if (candidate == topic) + { + return name; + } + } + return {}; + } + +} // namespace Progmasoft::Tazelish diff --git a/Sources/Help.cpp b/Sources/Help.cpp new file mode 100644 index 0000000..9febd19 --- /dev/null +++ b/Sources/Help.cpp @@ -0,0 +1,105 @@ +// SPDX-FileCopyrightText: 2026 Progmasoft <support@progmasoft.com> +// SPDX-License-Identifier: MPL-2.0 WITH AdditionRef-Progmasoft-Exception-2.0 + +#include "Progmasoft/Tazelish/Help.hpp" + +namespace Progmasoft::Tazelish +{ + namespace + { + + constexpr std::string_view kGeneral + = R"(Tazelish installs Tazel and chooses which version of it runs. + +Usage: tazelish <command> [arguments] + +Commands: + install <version> installs a version of Tazel + use [--force] <version> writes the version to ./tazelish.txt + default [--force] <version> + writes the version to the config.txt of the user + self update updates Tazelish + self remove removes Tazelish and the versions it installed + self purge also removes its settings, downloads and logs + help [command] prints this help, or the help of one command + +A version is MAJOR.SEMIMAJOR.MINOR.PATCH, unstable, nightly, +YYYY-MM-DD-unstable or YYYY-MM-DD-nightly. + +Exit codes: 0 success, 1 the work failed, 2 the command line was wrong. +)"; + + constexpr std::string_view kInstall + = R"(Usage: tazelish install <version> + +Installs a version of Tazel: + tazelish install 0.4.0.3 a release + tazelish install unstable the newest unstable build + tazelish install nightly the newest nightly build + tazelish install 2026-11-05-unstable the unstable build of a day + tazelish install 2026-11-05-nightly the nightly build of a day +)"; + + constexpr std::string_view kUse + = R"(Usage: tazelish use [--force] <version> + +Writes the version to tazelish.txt in the current folder, so Tazel runs that +version there and in the folders below. The nearest tazelish.txt comes before +the config.txt of the user. + +An existing tazelish.txt is an error; --force writes over it. +)"; + + constexpr std::string_view kDefault + = R"(Usage: tazelish default [--force] <version> + +Writes the version to the config.txt of the user, the version Tazel runs where +no tazelish.txt names one: + $HOME/.tazelish/config.txt on Linux and macOS + %USERPROFILE%\.tazelish\config.txt on Windows + +An existing config.txt is an error; --force writes over it. +)"; + + constexpr std::string_view kSelf + = R"(Usage: tazelish self update|remove|purge + + update updates Tazelish + remove removes Tazelish, the versions of Tazel it installed and the PATH + entries it added + purge does what remove does and also deletes the config.txt, the + downloads and the logs of Tazelish +)"; + + constexpr std::string_view kHelp = R"(Usage: tazelish help [command] + +Prints the help of Tazelish, or the help of one command: install, use, +default, self or help. +)"; + + } // namespace + + std::string_view + HelpText(std::optional<Topic> topic) + { + if (!topic) + { + return kGeneral; + } + switch (*topic) + { + case Topic::kInstall: + return kInstall; + case Topic::kUse: + return kUse; + case Topic::kDefault: + return kDefault; + case Topic::kSelf: + return kSelf; + case Topic::kHelp: + return kHelp; + } + return kGeneral; + } + +} // namespace Progmasoft::Tazelish diff --git a/Sources/Main.cpp b/Sources/Main.cpp new file mode 100644 index 0000000..8ea8b89 --- /dev/null +++ b/Sources/Main.cpp @@ -0,0 +1,140 @@ +// SPDX-FileCopyrightText: 2026 Progmasoft <support@progmasoft.com> +// SPDX-License-Identifier: MPL-2.0 WITH AdditionRef-Progmasoft-Exception-2.0 + +#include <cstdio> +#include <filesystem> +#include <fmt/format.h> +#include <string_view> +#include <vector> + +#include "Progmasoft/Tazelish/CommandLine.hpp" +#include "Progmasoft/Tazelish/Help.hpp" +#include "Progmasoft/Tazelish/VersionFiles.hpp" + +namespace +{ + + namespace fs = std::filesystem; + namespace tazelish = Progmasoft::Tazelish; + + constexpr int kSuccess = 0; + constexpr int kFailure = 1; + constexpr int kUsage = 2; + + int + Report(const tazelish::FileError &error) + { + fmt::print(stderr, + "error: {} {}\n", + error.file.string(), + error.message); + return kFailure; + } + + int + Write(const fs::path &file, const tazelish::Version &version, bool force) + { + if (auto error = tazelish::WriteVersionFile(file, version, force)) + { + return Report(*error); + } + fmt::print("Wrote {} to {}\n", + tazelish::FormatVersion(version), + file.string()); + return kSuccess; + } + + int + RunUse(const tazelish::Use &use) + { + return Write(fs::path(".") / fs::path(tazelish::kProjectFileName), + use.version, + use.force); + } + + int + RunDefault(const tazelish::Default &choice) + { + const auto home = tazelish::HomeDirectory(); + if (!home) + { +#ifdef _WIN32 + fmt::print(stderr, "error: USERPROFILE is not set\n"); +#else + fmt::print(stderr, "error: HOME is not set\n"); +#endif + return kFailure; + } + return Write(tazelish::ConfigFile(*home), choice.version, choice.force); + } + + // Installing and updating download from static.tazel.net, which this build + // of Tazelish does not do. + int + RunDownloading(std::string_view command) + { + fmt::print( + stderr, + "error: tazelish {} downloads from static.tazel.net, and this " + "build of Tazelish does not download\n", + command); + return kFailure; + } + + std::string_view + SelfName(tazelish::SelfAction action) + { + switch (action) + { + case tazelish::SelfAction::kUpdate: + return "self update"; + case tazelish::SelfAction::kRemove: + return "self remove"; + case tazelish::SelfAction::kPurge: + return "self purge"; + } + return "self"; + } + + int + Run(const tazelish::Command &command) + { + if (const auto *help = std::get_if<tazelish::Help>(&command)) + { + fmt::print("{}", tazelish::HelpText(help->topic)); + return kSuccess; + } + if (const auto *use = std::get_if<tazelish::Use>(&command)) + { + return RunUse(*use); + } + if (const auto *choice = std::get_if<tazelish::Default>(&command)) + { + return RunDefault(*choice); + } + if (std::holds_alternative<tazelish::Install>(command)) + { + return RunDownloading("install"); + } + return RunDownloading( + SelfName(std::get<tazelish::Self>(command).action)); + } + +} // namespace + +int +main(int argc, char **argv) +{ + std::vector<std::string_view> arguments; + for (int index = 1; index < argc; ++index) + { + arguments.emplace_back(argv[index]); + } + const auto parsed = tazelish::ParseCommandLine(arguments); + if (const auto *rejection = std::get_if<tazelish::Rejection>(&parsed)) + { + fmt::print(stderr, "error: {}\n", rejection->message); + return kUsage; + } + return Run(std::get<tazelish::Command>(parsed)); +} diff --git a/Sources/Version.cpp b/Sources/Version.cpp new file mode 100644 index 0000000..9bede40 --- /dev/null +++ b/Sources/Version.cpp @@ -0,0 +1,196 @@ +// SPDX-FileCopyrightText: 2026 Progmasoft <support@progmasoft.com> +// SPDX-License-Identifier: MPL-2.0 WITH AdditionRef-Progmasoft-Exception-2.0 + +#include <array> +#include <fmt/format.h> +#include <limits> + +#include "Progmasoft/Tazelish/Version.hpp" + +namespace Progmasoft::Tazelish +{ + namespace + { + + constexpr std::string_view kUnstable = "unstable"; + constexpr std::string_view kNightly = "nightly"; + + std::optional<Channel> + ParseChannel(std::string_view text) + { + if (text == kUnstable) + { + return Channel::kUnstable; + } + if (text == kNightly) + { + return Channel::kNightly; + } + return std::nullopt; + } + + std::string_view + ChannelName(Channel channel) + { + return channel == Channel::kUnstable ? kUnstable : kNightly; + } + + bool + IsDigit(char character) + { + return character >= '0' && character <= '9'; + } + + // A number of digits only, without a leading zero unless it is zero. + std::optional<std::uint32_t> + ParseNumber(std::string_view text) + { + if (text.empty() || (text.size() > 1 && text.front() == '0')) + { + return std::nullopt; + } + std::uint64_t value = 0; + for (const char character : text) + { + if (!IsDigit(character)) + { + return std::nullopt; + } + value + = value * 10 + static_cast<std::uint64_t>(character - '0'); + if (value > std::numeric_limits<std::uint32_t>::max()) + { + return std::nullopt; + } + } + return static_cast<std::uint32_t>(value); + } + + std::optional<Release> + ParseRelease(std::string_view text) + { + std::array<std::uint32_t, 4> numbers{}; + for (std::size_t index = 0; index < numbers.size(); ++index) + { + const std::size_t dot = text.find('.'); + const bool last = index + 1 == numbers.size(); + if (last != (dot == std::string_view::npos)) + { + return std::nullopt; + } + const auto number = ParseNumber(text.substr(0, dot)); + if (!number) + { + return std::nullopt; + } + numbers[index] = *number; + text = last ? std::string_view{} : text.substr(dot + 1); + } + return Release{ numbers[0], numbers[1], numbers[2], numbers[3] }; + } + + // Exactly [width] digits; leading zeros belong to a date. + std::optional<std::uint32_t> + ParseFixed(std::string_view text, std::size_t width) + { + if (text.size() != width) + { + return std::nullopt; + } + std::uint32_t value = 0; + for (const char character : text) + { + if (!IsDigit(character)) + { + return std::nullopt; + } + value + = value * 10 + static_cast<std::uint32_t>(character - '0'); + } + return value; + } + + bool + IsLeapYear(std::uint32_t year) + { + return (year % 4 == 0 && year % 100 != 0) || year % 400 == 0; + } + + std::uint32_t + DaysInMonth(std::uint32_t year, std::uint32_t month) + { + constexpr std::array<std::uint32_t, 12> kDays + = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; + return month == 2 && IsLeapYear(year) ? 29 : kDays[month - 1]; + } + + // YYYY-MM-DD-channel, with a date the calendar has. + std::optional<Dated> + ParseDated(std::string_view text) + { + constexpr std::size_t kDateLength = 10; + if (text.size() <= kDateLength + 1 || text[4] != '-' + || text[7] != '-' || text[kDateLength] != '-') + { + return std::nullopt; + } + const auto year = ParseFixed(text.substr(0, 4), 4); + const auto month = ParseFixed(text.substr(5, 2), 2); + const auto day = ParseFixed(text.substr(8, 2), 2); + const auto channel = ParseChannel(text.substr(kDateLength + 1)); + if (!year || !month || !day || !channel || *year == 0 || *month == 0 + || *month > 12 || *day == 0 + || *day > DaysInMonth(*year, *month)) + { + return std::nullopt; + } + return Dated{ static_cast<std::uint16_t>(*year), + static_cast<std::uint8_t>(*month), + static_cast<std::uint8_t>(*day), + *channel }; + } + + } // namespace + + std::optional<Version> + ParseVersion(std::string_view text) + { + if (const auto channel = ParseChannel(text)) + { + return Newest{ *channel }; + } + if (const auto release = ParseRelease(text)) + { + return *release; + } + if (const auto dated = ParseDated(text)) + { + return *dated; + } + return std::nullopt; + } + + std::string + FormatVersion(const Version &version) + { + if (const auto *release = std::get_if<Release>(&version)) + { + return fmt::format("{}.{}.{}.{}", + release->majorNumber, + release->semimajorNumber, + release->minorNumber, + release->patchNumber); + } + if (const auto *newest = std::get_if<Newest>(&version)) + { + return std::string(ChannelName(newest->channel)); + } + const auto &dated = std::get<Dated>(version); + return fmt::format("{:04}-{:02}-{:02}-{}", + dated.year, + dated.month, + dated.day, + ChannelName(dated.channel)); + } + +} // namespace Progmasoft::Tazelish diff --git a/Sources/VersionFiles.cpp b/Sources/VersionFiles.cpp new file mode 100644 index 0000000..2b11cee --- /dev/null +++ b/Sources/VersionFiles.cpp @@ -0,0 +1,195 @@ +// SPDX-FileCopyrightText: 2026 Progmasoft <support@progmasoft.com> +// SPDX-License-Identifier: MPL-2.0 WITH AdditionRef-Progmasoft-Exception-2.0 + +#include <cstdlib> +#include <fmt/format.h> +#include <fstream> +#include <system_error> +#include <utility> + +#include "Progmasoft/Tazelish/VersionFiles.hpp" + +namespace Progmasoft::Tazelish +{ + namespace + { + + namespace fs = std::filesystem; + + // A version file is short; anything longer names no version. + constexpr std::size_t kLongestContent = 64; + + FileError + Fail(const fs::path &file, std::string message) + { + return FileError{ file, std::move(message) }; + } + + // The text of a file without the line break and blanks around the + // version. + std::string_view + Trim(std::string_view text) + { + constexpr std::string_view kBlanks = " \t\r\n"; + constexpr std::string_view kByteOrderMark = "\xEF\xBB\xBF"; + if (text.substr(0, kByteOrderMark.size()) == kByteOrderMark) + { + text.remove_prefix(kByteOrderMark.size()); + } + const std::size_t first = text.find_first_not_of(kBlanks); + if (first == std::string_view::npos) + { + return {}; + } + const std::size_t last = text.find_last_not_of(kBlanks); + return text.substr(first, last - first + 1); + } + + } // namespace + + std::optional<fs::path> + HomeDirectory() + { +#ifdef _WIN32 + constexpr const char *kVariable = "USERPROFILE"; +#else + constexpr const char *kVariable = "HOME"; +#endif + // The environment is read once, before any thread starts. + const char *value + = std::getenv(kVariable); // NOLINT(concurrency-mt-unsafe) + if (value == nullptr || *value == '\0') + { + return std::nullopt; + } + return fs::path(value); + } + + fs::path + StateDirectory(const fs::path &home) + { + return home / fs::path(kStateDirectoryName); + } + + fs::path + ConfigFile(const fs::path &home) + { + return StateDirectory(home) / fs::path(kConfigFileName); + } + + std::variant<NoSelection, Version, FileError> + ReadVersionFile(const fs::path &file) + { + std::error_code error; + const fs::file_status status = fs::status(file, error); + if (status.type() == fs::file_type::not_found) + { + return NoSelection{}; + } + if (error) + { + return Fail(file, error.message()); + } + if (status.type() != fs::file_type::regular) + { + return Fail(file, "is not a regular file"); + } + std::ifstream stream(file, std::ios::binary); + if (!stream) + { + return Fail(file, "cannot be read"); + } + std::string content; + content.resize(kLongestContent + 1); + stream.read(content.data(), + static_cast<std::streamsize>(content.size())); + content.resize(static_cast<std::size_t>(stream.gcount())); + if (stream.bad()) + { + return Fail(file, "cannot be read"); + } + const std::string_view text = Trim(content); + if (content.size() > kLongestContent) + { + return Fail(file, "holds more than a version"); + } + if (auto version = ParseVersion(text)) + { + return *version; + } + return Fail(file, + fmt::format("holds '{}', which is not a version", text)); + } + + std::variant<NoSelection, Selection, FileError> + SelectVersion(const fs::path &directory, + const std::optional<fs::path> &home) + { + for (fs::path folder = directory;; folder = folder.parent_path()) + { + const fs::path file = folder / fs::path(kProjectFileName); + auto read = ReadVersionFile(file); + if (auto *failure = std::get_if<FileError>(&read)) + { + return std::move(*failure); + } + if (auto *version = std::get_if<Version>(&read)) + { + return Selection{ *version, file, VersionSource::kProject }; + } + if (folder == folder.parent_path()) + { + break; + } + } + if (!home) + { + return NoSelection{}; + } + const fs::path file = ConfigFile(*home); + auto read = ReadVersionFile(file); + if (auto *failure = std::get_if<FileError>(&read)) + { + return std::move(*failure); + } + if (auto *version = std::get_if<Version>(&read)) + { + return Selection{ *version, file, VersionSource::kUser }; + } + return NoSelection{}; + } + + std::optional<FileError> + WriteVersionFile(const fs::path &file, const Version &version, bool force) + { + std::error_code error; + const bool exists = fs::exists(fs::symlink_status(file, error)); + if (error && error != std::errc::no_such_file_or_directory) + { + return Fail(file, error.message()); + } + if (exists && !force) + { + return Fail(file, "already exists; --force writes over it"); + } + if (file.has_parent_path()) + { + fs::create_directories(file.parent_path(), error); + if (error) + { + return Fail(file.parent_path(), error.message()); + } + } + std::ofstream stream(file, std::ios::binary | std::ios::trunc); + const std::string content = FormatVersion(version) + "\n"; + stream.write(content.data(), + static_cast<std::streamsize>(content.size())); + stream.close(); + if (!stream) + { + return Fail(file, "cannot be written"); + } + return std::nullopt; + } + +} // namespace Progmasoft::Tazelish |