Skip to content

Commit

Permalink
We can actually build now!
Browse files Browse the repository at this point in the history
  • Loading branch information
Lomeli12 committed Aug 8, 2019
1 parent c82d087 commit 749802d
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 9 deletions.
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,22 @@ Beaver is a build system for Vala, written in Vala. It's goal is to be dead simp
## Why?

Because I wanted to learn how to use Vala and the build systems available were either ones I didn't like or weren't maintained anymore. Also because I'm mad.

## Build it from source

To build Beaver, you'll need to install the following dependencies.

* `glib-2.0`
* `json-glib-1.0`

Then, you can either build using ***beaver***.

```
beaver build
```

or use Valac

```
valac -o beaver --pkg json-glib-1.0 --pkg gio-2.0 src/main/vala/Beaver.vala src/main/vala/lib/*.vala src/main/vala/lib/logging/*.vala src/main/vala/beaver/*.vala src/main/vala/parse/*.vala
```
3 changes: 2 additions & 1 deletion build.beaver
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
# of choice
"dependencies": [
"gio-2.0",
"json-glib-1.0"
"json-glib-1.0",
"posix"
]
}
15 changes: 8 additions & 7 deletions src/main/vala/Beaver.vala
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,15 @@ public class Beaver {
}

static int build() {
var buildFile = Parser.getBuildFile();
if (buildFile != null) {
var mainFile = File.new_for_path(buildFile.getAppInfo().getMainFile());
if (!mainFile.query_exists()) {
stdout.printf(@"\033[31mCould not find main file: %s\033[0m", buildFile.getAppInfo().getMainFile());
return 0;
var beaverProject = Parser.getBuildFile();
if (beaverProject != null) {
stdout.printf(@"Starting Build\n");
if (BuildEnvironment.buildProject(beaverProject)) {
stdout.printf(@"\033[1;32m%s\033[0m %s", "BUILD SUCCESSFUL", "in IDK how many seconds.\n");
} else {
stdout.printf(@"\033[1;31m%s\033[0m %s", "BUILD FAILED", "in IDK how many seconds.\n");
return 1;
}
stdout.printf(@"Running valac\n");
} else {
stderr.printf(@"Invalid build.beaver.\n");
return 1;
Expand Down
88 changes: 87 additions & 1 deletion src/main/vala/beaver/BuildEnvironment.vala
Original file line number Diff line number Diff line change
@@ -1,3 +1,89 @@
public class BuildEnvironment {
static const string BEAVER_FOLDER = ".beaver";
public static const string BEAVER_FOLDER = ".beaver";
public static const string BUILD_FILE = "build.beaver";
public static const string BUILD_FOLDER = "build/";
public static const string SOURCE_FOLDER = "src/";
public static const string MAIN_FOLDER = SOURCE_FOLDER + "main/";
public static const string CODE_FOLDER = MAIN_FOLDER + "vala/";

public static bool buildProject(BeaverProject project) {
stdout.printf(@"Verifying project structure.\n");
if (!validateFolderStruct()) {
stdout.printf(@"Missing source folder. Ensure your code is located in \"src/main/vala\" of this directory.\n");
return false;
}
var mainFilePath = CODE_FOLDER + project.getAppInfo().getMainFile();
var mainFile = File.new_for_path(mainFilePath);
if (!mainFile.query_exists()) {
stdout.printf(@"\033[31mCould not find main file: %s\033[0m\n", project.getAppInfo().getMainFile());
return false;
}
stdout.printf(@"Locating source files...\n");
var sourceFiles = locateSourceFiles(CODE_FOLDER, mainFilePath, true);
var sources = new StringBuilder();
foreach (string file in sourceFiles) {
sources.append(file + " ");
}

stdout.printf(@"Creating preparing build...\n");
makeBuildFolder();
var output = "-o " + BUILD_FOLDER;
if (!StringUtil.isNullOrWhitespace(project.getAppInfo().getExecutableName())) {
output = output + project.getAppInfo().getExecutableName() + " ";
}

var deps = new StringBuilder();
foreach (string dep in project.getDependencies()) {
deps.append("--pkg " + dep + " ");
}

stdout.printf(@"Running Valac\n");
var command = "valac " + output + deps.str + sources.str;
var result = Posix.system(command);
return result == 0;
}

static string[] locateSourceFiles(string path, string mainFile, bool includeMain) {
string[] files = {};
if (includeMain) {
files += mainFile;
}
var sourceDir = Dir.open(path, 0);
var name = "";
var x = true;
while((name = sourceDir.read_name()) != null) {
var filePath = Path.build_filename(path, name);
if (filePath == mainFile)
continue;
if (FileUtils.test(filePath, FileTest.IS_REGULAR) && filePath.has_suffix(".vala")) {
files += filePath;
} else if (FileUtils.test(filePath, FileTest.IS_DIR)) {
var subDirFiles = locateSourceFiles(filePath, mainFile, false);
if (subDirFiles.length > 0) {
foreach (string file in subDirFiles) {
files += file;
}
}
}
}
return files;
}

public static bool validateFolderStruct() {
if (!FileUtils.test(SOURCE_FOLDER, FileTest.IS_DIR)) {
return false;
}
if (!FileUtils.test(MAIN_FOLDER, FileTest.IS_DIR)) {
return false;
}
if (!FileUtils.test(CODE_FOLDER, FileTest.IS_DIR)) {
return false;
}
return true;
}

static bool makeBuildFolder() {
return DirUtils.create_with_parents(BUILD_FOLDER, 0777) == 0;
}

}

0 comments on commit 749802d

Please sign in to comment.