Skip to content

Commit ef5e3ea

Browse files
Initial commit
0 parents  commit ef5e3ea

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+3664
-0
lines changed

.gitattributes

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* text eol=lf

.gitignore

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
/target
2+
**/*.rs.bk
3+
Cargo.lock
4+
5+
.vscode

.gitlab-ci.yml

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
image: 'rust:latest'
2+
3+
cache:
4+
paths:
5+
- cargo/
6+
7+
stages:
8+
- build
9+
- test
10+
11+
variables:
12+
CARGO_HOME: $CI_PROJECT_DIR/cargo
13+
14+
build:
15+
stage: build
16+
script:
17+
- rustc --version
18+
- cargo --version
19+
- cargo build --verbose
20+
artifacts:
21+
expire_in: 1 hour
22+
paths:
23+
- cargo/
24+
only:
25+
- master
26+
- merge_requests
27+
28+
test:
29+
stage: test
30+
script:
31+
- cargo build --verbose
32+
- cargo test --verbose
33+
only:
34+
- master
35+
- merge_requests
36+
37+
minify-roact-test:
38+
stage: test
39+
before_script:
40+
- apt-get -qq update
41+
- apt-get -qq install -y python python-pip
42+
- python --version
43+
- chmod u+x ./bin/install-lua.sh
44+
- ./bin/install-lua.sh
45+
- export PATH=$PATH:$PWD/lua_install/bin
46+
script:
47+
- chmod u+x ./bin/test-minify-with-roact.sh
48+
- ./bin/test-minify-with-roact.sh
49+
only:
50+
- master
51+
- merge_requests

Cargo.toml

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
[package]
2+
name = "darklua"
3+
version = "0.1.0"
4+
authors = ["jeparlefrancais <jeparlefrancais21@gmail.com>"]
5+
edition = "2018"
6+
readme = "README.md"
7+
description = "Obfuscate Lua 5.1 scripts"
8+
repository = "https://gitlab.com/jeparlefrancais/darklua"
9+
license = "MIT"
10+
keywords = ["lua", "obsfucation", "minify"]
11+
12+
[badges]
13+
gitlab = { repository = "jeparlefrancais/darklua" }
14+
15+
[lib]
16+
name = "darklua_core"
17+
path = "src/lib.rs"
18+
19+
[[bin]]
20+
name = "darklua"
21+
path = "src/bin.rs"
22+
23+
[dependencies]
24+
structopt = "0.3.9"
25+
luaparser = { version = "0.1.0", default-features = false }
26+
27+
[dev-dependencies]
28+
insta = "0.12"

LICENSE.txt

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2020 jeparlefrancais
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
[![pipeline status](https://gitlab.com/jeparlefrancais/darklua/badges/master/pipeline.svg)](https://gitlab.com/jeparlefrancais/darklua/commits/master)
2+
3+
# darklua
4+
5+
Obfuscate Lua 5.1 scripts.
6+
7+
8+
# Usage
9+
10+
The following section will detail the different commands of darklua. You can also get a list of the available commands and options using the command line tool itself, simply run:
11+
```
12+
darklua help
13+
```
14+
To get help on a specific command, simply append `--help` (or `-h`) to the command name. For example, to get help on the `minify` command:
15+
```
16+
darklua minify --help
17+
```
18+
19+
## Minify
20+
21+
This command reads Lua code and only reformat it in a more compact way. The input path can be a file or directory. Given a directory, darklua will find all Lua files under that directory and output them following the same hierarchy.
22+
23+
```
24+
darklua minify <input-path> <output-path>
25+
26+
optional arguments:
27+
-c, --column-span <int> : default to 80
28+
Amount of characters before the code is wrapped into a new line
29+
```
30+
31+
### Example
32+
33+
If you have a `src` folder that contains a bunch of Lua scripts (files ending with `.lua`), you can generate the minified version of these scripts into a new folder called `minified-src` using the following command:
34+
35+
```
36+
darklua minify src minified-src
37+
```
38+
39+
To specify the column-span argument, simply run:
40+
41+
```
42+
darklua minify src minified-src --column-span 120
43+
# or the shorter version:
44+
darklua minify src minified-src -c 120
45+
```
46+
47+
48+
# Installation
49+
50+
darklua is a command line tool that can be installed using [cargo](https://doc.rust-lang.org/cargo/getting-started/installation.html).
51+
52+
```
53+
cargo install darklua
54+
```
55+
56+
If you want to use the lastest darklua available, install it using the git url:
57+
58+
```
59+
cargo install --git https://gitlab.com/jeparlefrancais/darklua.git
60+
```
61+
62+
63+
# License
64+
65+
darklua is available under the MIT license. See [LICENSE.txt](LICENSE.txt) for details.

bin/install-lua.sh

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#!/bin/sh
2+
3+
python -m pip install hererocks
4+
hererocks lua_install -r^ --lua=5.1
5+
export PATH=$PATH:$PWD/lua_install/bin
6+
7+
luarocks install luafilesystem
8+
luarocks install busted
9+
luarocks install luacheck

bin/test-minify-with-roact.sh

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#!/bin/sh
2+
3+
# clone a known working version of roact
4+
git clone https://github.com/Roblox/roact.git
5+
cd roact
6+
git checkout d9b7f9661b26ff16db240f2fe8b0f8284303c61d
7+
git submodule init
8+
git submodule update
9+
cd ..
10+
11+
# minify the source
12+
cargo run -- minify roact/src roact-minify/src
13+
14+
# copy the submodules and the bin to be able to execute tests
15+
cp roact/bin roact-minify/bin -r
16+
cp roact/modules roact-minify/modules -r
17+
18+
# run Lua tests
19+
cd roact-minify
20+
lua bin/spec.lua

src/bin.rs

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
mod cli;
2+
3+
use cli::{Command, Darklua};
4+
use structopt::StructOpt;
5+
6+
fn main() {
7+
let darklua = Darklua::from_args();
8+
let global_options = darklua.global_options;
9+
10+
match darklua.command {
11+
Command::Minify(options) => cli::minify::run(&options, &global_options),
12+
};
13+
}

src/cli/minify.rs

+121
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
use crate::cli::GlobalOptions;
2+
use crate::cli::utils::{
3+
maybe_plural,
4+
write_file,
5+
FileProcessing,
6+
};
7+
8+
use darklua_core::{LuaGenerator, ToLua, Parser, ParsingError};
9+
use std::fmt::{self, Display};
10+
use std::path::PathBuf;
11+
use std::fs;
12+
use std::process;
13+
use structopt::StructOpt;
14+
15+
#[derive(Debug, StructOpt)]
16+
pub struct Options {
17+
/// Path to the lua file to minify.
18+
#[structopt(parse(from_os_str))]
19+
pub input_path: PathBuf,
20+
/// Where to output the result.
21+
#[structopt(parse(from_os_str))]
22+
pub output_path: PathBuf,
23+
/// Amount of characters before the code is wrapped into a new line.
24+
#[structopt(long, short, default_value = "80")]
25+
pub column_span: usize,
26+
}
27+
28+
pub enum Error {
29+
Parser(PathBuf, ParsingError),
30+
InputFile(String),
31+
InputFileNotFound(PathBuf),
32+
OutputFile(PathBuf, String),
33+
}
34+
35+
impl Display for Error {
36+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
37+
match self {
38+
Self::Parser(path, error) => {
39+
write!(f, "could not parse input at <{}>: {:?}", path.to_string_lossy(), error)
40+
}
41+
Self::InputFile(error) => {
42+
write!(f, "error while reading input file: {}", error)
43+
}
44+
Self::InputFileNotFound(path) => {
45+
write!(f, "input file not found: {}", path.to_string_lossy())
46+
}
47+
Self::OutputFile(path, error) => {
48+
write!(f, "error with output file <{}>: {}", path.to_string_lossy(), error)
49+
}
50+
}
51+
}
52+
}
53+
54+
type MinifyResult = Result<(), Error>;
55+
56+
fn process(file: &FileProcessing, options: &Options, global: &GlobalOptions) -> MinifyResult {
57+
let source = &file.source;
58+
let output = &file.output;
59+
60+
if !source.exists() {
61+
return Err(Error::InputFileNotFound(source.clone()))
62+
}
63+
64+
let input = fs::read_to_string(source)
65+
.map_err(|io_error| Error::InputFile(format!("{}", io_error)))?;
66+
67+
let parser = Parser::default();
68+
69+
let block = parser.parse(&input)
70+
.map_err(|parser_error| Error::Parser(source.clone(), parser_error))?;
71+
72+
let mut generator = LuaGenerator::new(options.column_span);
73+
block.to_lua(&mut generator);
74+
let minified = generator.into_string();
75+
76+
write_file(&output, &minified)
77+
.map_err(|io_error| Error::OutputFile(output.clone(), format!("{}", io_error)))?;
78+
79+
if global.verbose > 0 {
80+
println!("Successfully processed <{}>", source.to_string_lossy());
81+
};
82+
83+
Ok(())
84+
}
85+
86+
pub fn run(options: &Options, global: &GlobalOptions) {
87+
let file = FileProcessing::find(&options.input_path, &options.output_path, global);
88+
89+
let results: Vec<Result<(), Error>> = file.iter()
90+
.map(|file_processing| process(file_processing, &options, global))
91+
.collect();
92+
93+
let total_files = results.len();
94+
95+
let errors: Vec<Error> = results.into_iter()
96+
.filter_map(|result| match result {
97+
Ok(()) => {
98+
None
99+
}
100+
Err(error) => Some(error),
101+
})
102+
.collect();
103+
104+
let error_count = errors.len();
105+
106+
if error_count == 0 {
107+
println!("Successfully minified {} file{}", total_files, maybe_plural(total_files));
108+
109+
} else {
110+
let success_count = total_files - error_count;
111+
112+
if success_count > 0 {
113+
eprintln!("Successfully minified {} file{}.", success_count, maybe_plural(success_count));
114+
}
115+
116+
eprintln!("But {} error{} happened:", error_count, maybe_plural(error_count));
117+
118+
errors.iter().for_each(|error| eprintln!("-> {}", error));
119+
process::exit(1);
120+
}
121+
}

src/cli/mod.rs

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
pub mod minify;
2+
pub mod utils;
3+
4+
use structopt::StructOpt;
5+
6+
#[derive(Debug, StructOpt)]
7+
pub struct GlobalOptions {
8+
/// Sets verbosity level (can be specified multiple times)
9+
#[structopt(long, short, global(true), parse(from_occurrences))]
10+
pub verbose: u8,
11+
}
12+
13+
#[derive(Debug, StructOpt)]
14+
pub enum Command {
15+
/// Minify lua files
16+
Minify(minify::Options),
17+
}
18+
19+
#[derive(Debug, StructOpt)]
20+
#[structopt(name = "darklua", about, author)]
21+
pub struct Darklua {
22+
#[structopt(flatten)]
23+
pub global_options: GlobalOptions,
24+
/// The command to run. For specific help about each command, run `darklua <command> --help`
25+
#[structopt(subcommand)]
26+
pub command: Command,
27+
}

0 commit comments

Comments
 (0)