Skip to content

Commit 296d56a

Browse files
authored
refactor(generator): unify multiple definitions of MakeDirectory() (#13762)
Fixes #12818.
1 parent 1ee3e86 commit 296d56a

File tree

7 files changed

+23
-46
lines changed

7 files changed

+23
-46
lines changed

generator/internal/codegen_utils.cc

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@
2626
#include <cctype>
2727
#include <string>
2828
#include <unordered_set>
29+
#if _WIN32
30+
#include <direct.h>
31+
#else
32+
#include <sys/stat.h>
33+
#endif // _WIN32
2934

3035
namespace google {
3136
namespace cloud {
@@ -380,6 +385,14 @@ std::string FormatHeaderIncludeGuard(absl::string_view header_path) {
380385
{{"/", "_"}, {".", "_"}}));
381386
}
382387

388+
void MakeDirectory(std::string const& path) {
389+
#if _WIN32
390+
_mkdir(path.c_str());
391+
#else
392+
mkdir(path.c_str(), 0755);
393+
#endif // _WIN32
394+
}
395+
383396
} // namespace generator_internal
384397
} // namespace cloud
385398
} // namespace google

generator/internal/codegen_utils.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,9 @@ std::string FormatCommentKeyValueList(
130130
// Formats a header include guard per the provided header_path.
131131
std::string FormatHeaderIncludeGuard(absl::string_view header_path);
132132

133+
/// Create a directory. The parent must exist.
134+
void MakeDirectory(std::string const& path);
135+
133136
} // namespace generator_internal
134137
} // namespace cloud
135138
} // namespace google

generator/internal/discovery_file.cc

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,25 +19,12 @@
1919
#include <google/protobuf/io/printer.h>
2020
#include <google/protobuf/io/zero_copy_stream_impl.h>
2121
#include <fstream>
22-
#ifdef _WIN32
23-
#include <direct.h>
24-
#else
25-
#include <sys/stat.h>
26-
#endif // _WIN32
2722

2823
namespace google {
2924
namespace cloud {
3025
namespace generator_internal {
3126
namespace {
3227

33-
void MakeDirectory(std::string const& path) {
34-
#if _WIN32
35-
_mkdir(path.c_str());
36-
#else
37-
mkdir(path.c_str(), 0777);
38-
#endif // _WIN32
39-
}
40-
4128
std::string GeneratedProtoPreamble() {
4229
auto constexpr kPreamble = R"""(
4330
// Generated by the C++ microgenerator.

generator/internal/discovery_proto_export_file.cc

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -19,27 +19,10 @@
1919
#include <google/protobuf/io/printer.h>
2020
#include <google/protobuf/io/zero_copy_stream_impl.h>
2121
#include <fstream>
22-
#ifdef _WIN32
23-
#include <direct.h>
24-
#else
25-
#include <sys/stat.h>
26-
#endif // _WIN32
2722

2823
namespace google {
2924
namespace cloud {
3025
namespace generator_internal {
31-
namespace {
32-
33-
// TODO(#12818): refactor this function to single translation unit.
34-
void MakeDirectory(std::string const& path) {
35-
#if _WIN32
36-
_mkdir(path.c_str());
37-
#else
38-
mkdir(path.c_str(), 0777);
39-
#endif // _WIN32
40-
}
41-
42-
} // namespace
4326

4427
DiscoveryProtoExportFile::DiscoveryProtoExportFile(
4528
std::string output_file_path, std::string relative_file_path,

generator/internal/scaffold_generator.cc

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
// limitations under the License.
1414

1515
#include "generator/internal/scaffold_generator.h"
16+
#include "generator/internal/codegen_utils.h"
1617
#include "google/cloud/internal/absl_str_join_quiet.h"
1718
#include "google/cloud/internal/absl_str_replace_quiet.h"
1819
#include "google/cloud/internal/filesystem.h"
@@ -25,11 +26,6 @@
2526
#include <fstream>
2627
#include <iterator>
2728
#include <regex>
28-
#ifdef _WIN32
29-
#include <direct.h>
30-
#else
31-
#include <sys/stat.h>
32-
#endif // _WIN32
3329

3430
namespace google {
3531
namespace cloud {
@@ -224,14 +220,6 @@ std::string ServiceConfigYamlPath(
224220
return absl::StrCat(root, "/", name->second);
225221
}
226222

227-
void MakeDirectory(std::string const& path) {
228-
#if _WIN32
229-
_mkdir(path.c_str());
230-
#else
231-
mkdir(path.c_str(), 0755);
232-
#endif // _WIN32
233-
}
234-
235223
void GenerateMetadata(
236224
std::map<std::string, std::string> const& vars,
237225
std::string const& output_path,

generator/internal/scaffold_generator.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,9 +92,6 @@ std::map<std::string, std::string> ScaffoldVars(
9292
std::string ServiceConfigYamlPath(
9393
std::string const& root, std::map<std::string, std::string> const& vars);
9494

95-
/// Create a directory. The parent must exist.
96-
void MakeDirectory(std::string const& path);
97-
9895
/**
9996
* Generates (if possible) a `.repo-metadata.json` file for @p service.
10097
*

generator/internal/scaffold_generator_test.cc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,17 @@
1313
// limitations under the License.
1414

1515
#include "generator/internal/scaffold_generator.h"
16+
#include "generator/internal/codegen_utils.h"
1617
#include "google/cloud/internal/random.h"
1718
#include <gmock/gmock.h>
1819
#include <cstdlib>
1920
#include <fstream>
2021
#include <sstream>
22+
#if _WIN32
23+
#include <direct.h>
24+
#else
25+
#include <unistd.h>
26+
#endif // _WIN32
2127

2228
namespace google {
2329
namespace cloud {

0 commit comments

Comments
 (0)