Skip to content

Commit

Permalink
refactor(generator): unify multiple definitions of MakeDirectory() (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
devbww authored Mar 12, 2024
1 parent 1ee3e86 commit 296d56a
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 46 deletions.
13 changes: 13 additions & 0 deletions generator/internal/codegen_utils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@
#include <cctype>
#include <string>
#include <unordered_set>
#if _WIN32
#include <direct.h>
#else
#include <sys/stat.h>
#endif // _WIN32

namespace google {
namespace cloud {
Expand Down Expand Up @@ -380,6 +385,14 @@ std::string FormatHeaderIncludeGuard(absl::string_view header_path) {
{{"/", "_"}, {".", "_"}}));
}

void MakeDirectory(std::string const& path) {
#if _WIN32
_mkdir(path.c_str());
#else
mkdir(path.c_str(), 0755);
#endif // _WIN32
}

} // namespace generator_internal
} // namespace cloud
} // namespace google
3 changes: 3 additions & 0 deletions generator/internal/codegen_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,9 @@ std::string FormatCommentKeyValueList(
// Formats a header include guard per the provided header_path.
std::string FormatHeaderIncludeGuard(absl::string_view header_path);

/// Create a directory. The parent must exist.
void MakeDirectory(std::string const& path);

} // namespace generator_internal
} // namespace cloud
} // namespace google
Expand Down
13 changes: 0 additions & 13 deletions generator/internal/discovery_file.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,25 +19,12 @@
#include <google/protobuf/io/printer.h>
#include <google/protobuf/io/zero_copy_stream_impl.h>
#include <fstream>
#ifdef _WIN32
#include <direct.h>
#else
#include <sys/stat.h>
#endif // _WIN32

namespace google {
namespace cloud {
namespace generator_internal {
namespace {

void MakeDirectory(std::string const& path) {
#if _WIN32
_mkdir(path.c_str());
#else
mkdir(path.c_str(), 0777);
#endif // _WIN32
}

std::string GeneratedProtoPreamble() {
auto constexpr kPreamble = R"""(
// Generated by the C++ microgenerator.
Expand Down
17 changes: 0 additions & 17 deletions generator/internal/discovery_proto_export_file.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,27 +19,10 @@
#include <google/protobuf/io/printer.h>
#include <google/protobuf/io/zero_copy_stream_impl.h>
#include <fstream>
#ifdef _WIN32
#include <direct.h>
#else
#include <sys/stat.h>
#endif // _WIN32

namespace google {
namespace cloud {
namespace generator_internal {
namespace {

// TODO(#12818): refactor this function to single translation unit.
void MakeDirectory(std::string const& path) {
#if _WIN32
_mkdir(path.c_str());
#else
mkdir(path.c_str(), 0777);
#endif // _WIN32
}

} // namespace

DiscoveryProtoExportFile::DiscoveryProtoExportFile(
std::string output_file_path, std::string relative_file_path,
Expand Down
14 changes: 1 addition & 13 deletions generator/internal/scaffold_generator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
// limitations under the License.

#include "generator/internal/scaffold_generator.h"
#include "generator/internal/codegen_utils.h"
#include "google/cloud/internal/absl_str_join_quiet.h"
#include "google/cloud/internal/absl_str_replace_quiet.h"
#include "google/cloud/internal/filesystem.h"
Expand All @@ -25,11 +26,6 @@
#include <fstream>
#include <iterator>
#include <regex>
#ifdef _WIN32
#include <direct.h>
#else
#include <sys/stat.h>
#endif // _WIN32

namespace google {
namespace cloud {
Expand Down Expand Up @@ -224,14 +220,6 @@ std::string ServiceConfigYamlPath(
return absl::StrCat(root, "/", name->second);
}

void MakeDirectory(std::string const& path) {
#if _WIN32
_mkdir(path.c_str());
#else
mkdir(path.c_str(), 0755);
#endif // _WIN32
}

void GenerateMetadata(
std::map<std::string, std::string> const& vars,
std::string const& output_path,
Expand Down
3 changes: 0 additions & 3 deletions generator/internal/scaffold_generator.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,6 @@ std::map<std::string, std::string> ScaffoldVars(
std::string ServiceConfigYamlPath(
std::string const& root, std::map<std::string, std::string> const& vars);

/// Create a directory. The parent must exist.
void MakeDirectory(std::string const& path);

/**
* Generates (if possible) a `.repo-metadata.json` file for @p service.
*
Expand Down
6 changes: 6 additions & 0 deletions generator/internal/scaffold_generator_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,17 @@
// limitations under the License.

#include "generator/internal/scaffold_generator.h"
#include "generator/internal/codegen_utils.h"
#include "google/cloud/internal/random.h"
#include <gmock/gmock.h>
#include <cstdlib>
#include <fstream>
#include <sstream>
#if _WIN32
#include <direct.h>
#else
#include <unistd.h>
#endif // _WIN32

namespace google {
namespace cloud {
Expand Down

0 comments on commit 296d56a

Please sign in to comment.