Skip to content

Commit 4fb2dc7

Browse files
committed
Change: Separate path functions from utils and main function from luaumb
- Renamed from .h -> .hpp - Moved path functions from utils to path.hpp - Moved main function out of luaumb (this commit only moves the code), future plans include handling errors/results via return functions and making luaumb usable through the header. - Added version submodule to display in --help - Updated readme
1 parent 865bad4 commit 4fb2dc7

File tree

13 files changed

+172
-111
lines changed

13 files changed

+172
-111
lines changed

CMakeLists.txt

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,16 @@ add_subdirectory(submodules/luau)
88

99
include_directories(submodules/base64/include)
1010

11-
add_executable(luaumb src/luaumb.cpp src/localizer.cpp src/bundle.cpp src/utils.cpp)
11+
add_executable(
12+
luaumb
13+
14+
src/main.cpp
15+
src/path.cpp
16+
src/luaumb.cpp
17+
src/localizer.cpp
18+
src/bundle.cpp
19+
src/utils.cpp
20+
)
1221

1322
if (MSVC)
1423
target_compile_options(luaumb PRIVATE /W4)

README.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ cmake .. && cmake --build . -j $(nproc)
1414
## Usage
1515

1616
```txt
17-
Luau module bundle - v0.1
17+
Luau module bundle - 0.1-dev luau:0.658-release base64:387b32f-checkout
1818
luaumb [main.luau] [out.luau]
1919
```
2020

@@ -24,7 +24,6 @@ Luau module bundle - v0.1
2424
mkdir -p tmp
2525
./luaumb ./example/main.luau ./tmp/out.luau
2626
27-
Modules and dependencies
2827
Modules and dependencies
2928
/lib/count.luau
3029
/lib/print.luau

src/bundle.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#include "bundle.h"
1+
#include "bundle.hpp"
22

33
#include <sstream>
44
#include <queue>

src/bundle.h renamed to src/bundle.hpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@
66
#include <map>
77
#include <filesystem>
88

9-
#include "localizer.h"
10-
#include "utils.h"
9+
#include "path.hpp"
10+
#include "localizer.hpp"
11+
#include "utils.hpp"
1112

1213
struct ModuleFile {
1314
std::string name;

src/localizer.cpp

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#include "localizer.h"
1+
#include "localizer.hpp"
22

33
#include <string>
44
#include <sstream>
@@ -25,12 +25,6 @@ Location::operator std::string() const {
2525
return ss.str();
2626
}
2727

28-
void luau_fvalue_init() {
29-
for (Luau::FValue<bool>* flag = Luau::FValue<bool>::list; flag; flag = flag->next) {
30-
if (strncmp(flag->name, "Luau", 4) == 0) flag->value = true;
31-
}
32-
}
33-
3428
struct FindRequireConstantString : public Luau::AstVisitor {
3529
std::vector<ExprCallRequire> list;
3630

src/localizer.h renamed to src/localizer.hpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
#include "Luau/Location.h"
88
#include "Luau/ParseResult.h"
99

10-
#include "utils.h"
10+
#include "utils.hpp"
1111

1212
struct Location {
1313
unsigned int begin_line, begin_column, end_line, end_column;
@@ -27,7 +27,6 @@ struct RequireFunctionLocalizerResult {
2727
std::vector<ExprCallRequire> list;
2828
};
2929

30-
void luau_fvalue_init();
3130
RequireFunctionLocalizerResult require_function_localizer(const std::string& source);
3231

3332
#endif /* LUAUMB_LOCALIZER_H */

src/luaumb.cpp

Lines changed: 25 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -8,33 +8,34 @@
88
#include <optional>
99
#include <fstream>
1010

11-
#include "Luau/ToString.h"
1211
#include "Luau/FileUtils.h"
1312
#include "Luau/Config.h"
1413

15-
#include "localizer.h"
16-
#include "bundle.h"
17-
#include "utils.h"
14+
#include "luaumb.hpp"
15+
#include "path.hpp"
16+
#include "localizer.hpp"
17+
#include "bundle.hpp"
18+
#include "utils.hpp"
1819

19-
#ifndef LUAUMB_VERSION
20-
#define LUAUMB_VERSION "0.1"
21-
#endif
20+
bool fvalue_inited = false;
2221

23-
int main(int argc, char** argv) {
24-
luau_fvalue_init();
25-
26-
if ((argc < 3) || (argc >= 3 && strcmp(argv[1], "--help") == 0)) {
27-
std::cout << "Luau module bundle - v" << LUAUMB_VERSION << std::endl;
28-
std::cout << " luaumb [main.luau] [out.luau]" << std::endl;
29-
return 0;
22+
void luau_fvalue_init() {
23+
for (Luau::FValue<bool>* flag = Luau::FValue<bool>::list; flag; flag = flag->next) {
24+
if (strncmp(flag->name, "Luau", 4) == 0) flag->value = true;
3025
}
26+
}
3127

32-
const char* in_name = argv[1];
33-
const char* out_name = argv[2];
28+
// TODO: handle error/result by returning it instead of outputting it to stdout.
29+
30+
void luaumb::bundle(const std::string& main_file, const std::string& out_file) {
31+
if (!fvalue_inited) {
32+
luau_fvalue_init();
33+
fvalue_inited = true;
34+
}
3435

3536
std::map<const std::string, std::vector<Luau::ParseError>> parse_errors;
3637
std::map<std::string, Luau::Config> configs;
37-
RelativePathModule main_path(in_name);
38+
RelativePathModule main_path(main_file);
3839
LuauModuleBundle lmb(main_path);
3940
std::queue<RelativePathModule> q;
4041
q.push(main_path);
@@ -49,7 +50,7 @@ int main(int argc, char** argv) {
4950

5051
if (!file) {
5152
std::cout << "Couldn't read source " << module_path.path << std::endl;
52-
return 1;
53+
return; // TODO: Return error
5354
}
5455

5556
std::filesystem::path relative_path = module_path.relative.parent_path();
@@ -134,13 +135,11 @@ int main(int argc, char** argv) {
134135
}
135136

136137
const std::string out = lmb.build();
137-
std::ofstream out_file(out_name);
138-
if (!out_file) {
139-
std::cout << "Couldn't open output file " << out_name << std::endl;
140-
return 1;
138+
std::ofstream out_fs(out_file);
139+
if (!out_fs) {
140+
std::cout << "Couldn't open output file " << out_file << std::endl;
141+
return; // TODO: Return error
141142
}
142-
out_file << out;
143-
out_file.close();
144-
145-
return parse_errors.size() > 0 ? 1 : 0;
143+
out_fs << out;
144+
out_fs.close();
146145
}

src/luaumb.hpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#ifndef LUAUMB_H
2+
#define LUAUMB_H
3+
4+
#include <string>
5+
6+
#ifndef LUAUMB_VERSION
7+
#define LUAUMB_VERSION "0.1-dev"
8+
#endif
9+
10+
#ifndef LUAUMB_LUAU_VERSION
11+
#define LUAUMB_LUAU_VERSION "0.658-release"
12+
#endif
13+
14+
#ifndef LUAUMB_LIB_BASE64_VERSION
15+
#define LUAUMB_LIB_BASE64_VERSION "387b32f-checkout"
16+
#endif
17+
18+
namespace luaumb {
19+
const struct {
20+
const std::string luaumb = LUAUMB_VERSION;
21+
const std::string luau = LUAUMB_LUAU_VERSION;
22+
const std::string base64 = LUAUMB_LIB_BASE64_VERSION;
23+
} version;
24+
25+
void bundle(const std::string& main_file, const std::string& out_file);
26+
}
27+
28+
#endif /* LUAUMB_H */

src/main.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#include <iostream>
2+
#include <string>
3+
#include <cstring>
4+
5+
#include "luaumb.hpp"
6+
7+
int main(int argc, char** argv) {
8+
if ((argc < 3) || (argc >= 3 && strcmp(argv[1], "--help") == 0)) {
9+
std::cout << "Luau module bundle - " << luaumb::version.luaumb << " luau:" << luaumb::version.luau << " base64:" << luaumb::version.base64 << std::endl;
10+
std::cout << " luaumb [main.luau] [out.luau]" << std::endl;
11+
12+
return 0;
13+
}
14+
15+
const std::string in_file = argv[1];
16+
const std::string out_file = argv[2];
17+
18+
luaumb::bundle(in_file, out_file);
19+
20+
return 0;
21+
}

src/path.cpp

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
#include "path.hpp"
2+
3+
#include <array>
4+
5+
using namespace std;
6+
7+
namespace fs = filesystem;
8+
9+
const std::array<const string, 4> module_suffixes = {".luau", ".lua", "/init.luau", "/init.lua"};
10+
11+
RelativePathModule::RelativePathModule(const fs::path& root, const fs::path& relative, const fs::path& path) {
12+
this->root = root;
13+
this->relative = relative.is_absolute() ? relative : fs::path("/") / relative;
14+
this->path = path;
15+
}
16+
17+
RelativePathModule::RelativePathModule(const fs::path& main_path) {
18+
this->path = main_path.lexically_normal();
19+
fs::path root_relative("/");
20+
root_relative += main_path.filename();
21+
this->relative = root_relative;
22+
this->root = main_path.parent_path();
23+
24+
if (!fs::exists(this->path)) {
25+
throw runtime_error("Error main file not found\n File: " + this->path.string());
26+
}
27+
}
28+
29+
RelativePathModule::operator string() const {
30+
stringstream ss;
31+
ss << "[ROOT:\"" << this->root.string() << "\" PATH:\"" << this->path.string() << "\" RELATIVE:\"" << this->relative.string() << "\"]";
32+
33+
return ss.str();
34+
}
35+
36+
RelativePathModule relative_path_module(const RelativePathModule& parent_path, const fs::path& module_path) {
37+
fs::path path = (parent_path.path.parent_path() / module_path).lexically_normal();
38+
39+
bool found = false;
40+
for (string ext : module_suffixes) {
41+
fs::path _path = path;
42+
_path += ext;
43+
if (fs::exists(_path)) {
44+
found = true;
45+
path = _path;
46+
break;
47+
}
48+
}
49+
50+
if (!found) throw runtime_error("Error module file not found\n Parent: " + string(parent_path) + "\n File: " + path.string());
51+
52+
return RelativePathModule(parent_path.root, path.lexically_proximate(parent_path.root), path);
53+
}

0 commit comments

Comments
 (0)