Skip to content

Commit

Permalink
add doc comments to the build script
Browse files Browse the repository at this point in the history
  • Loading branch information
omdxp committed Jun 14, 2024
1 parent ffde5bc commit 6f8be0a
Showing 1 changed file with 57 additions and 12 deletions.
69 changes: 57 additions & 12 deletions d/build/source/app.d
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
/++
This module is responsible for generating the data file from the info.json files.
+/
module app;

import std.file : readText, write, exists, remove;
Expand All @@ -6,11 +9,19 @@ import asdf;
import std.stdio : writeln, File;
import std.algorithm;
import std.array;
import std.regex;

const string dataPath = "../../_data/";
const string dataFile = "../source/kuliya/data.d";
/++
Data path to the info.json files
+/
const string DATA_PATH = "../../_data/";
/++
Data D file to save the schema
+/
const string DATA_FILE = "../source/kuliya/data.d";

/++
Kuliya types
+/
enum Type
{
UNIVERSITY,
Expand All @@ -23,35 +34,58 @@ enum Type
SECTOR
}

/++
Kuliya name
+/
struct Name
{
/// Arabic name
string ar;
/// English name
string en;
/// French name
string fr;
}

/++
Kuliya terms
+/
struct Terms
{
/// Number of slots per year
int perYear;
/// Slots
int[] slots;
}

/++
Kuliya schema
+/
struct Schema
{
/// Name
Name name;
/// Type
Type type;
/// Terms
@serdeOptional
Nullable!Terms terms;
}

/++
Save schema to file
Params:
schema = Schema to save
path = Path of the info.json file
+/
void saveToFile(const Schema schema, string path)
{
// remove ../_data/ prefix and /info.json suffix from path
const auto pathParts = path.splitter('/').array;
const auto filePath = pathParts[3 .. $ - 1].join("/");
// replace / with _ in filePath
const auto filePathUnderscored = filePath.replace("/", "_");
File file = File(dataFile, "a");
File file = File(DATA_FILE, "a");
file.writeln("immutable Schema ", filePathUnderscored, " = ");
file.writeln("{");
file.writeln(" Name(\"", schema.name.ar, "\", \"", schema.name.en, "\", \"", schema.name.fr, "\"),");
Expand All @@ -76,6 +110,13 @@ void saveToFile(const Schema schema, string path)
file.close();
}

/++
Parse info.json file to Schema struct
Params:
path = Path of the info.json file
Returns:
Schema
+/
Schema parseInfoJson(string path)
{
const auto data = readText(path);
Expand All @@ -84,6 +125,11 @@ Schema parseInfoJson(string path)
return schema;
}

/++
Walk through directories and parse info.json files
Params:
path = Path of the directory
+/
void walkDirs(string path)
{
import std.file : DirEntry, dirEntries, isDir, isFile, SpanMode;
Expand All @@ -102,9 +148,12 @@ void walkDirs(string path)
}
}

/++
Prepare data file with the schema structure
+/
void prepareDataFile()
{
File file = File(dataFile, "w");
File file = File(DATA_FILE, "w");
file.writeln("// This is an auto generated file, do not edit it!");
file.writeln("module kuliya.data;");
file.writeln();
Expand Down Expand Up @@ -147,17 +196,13 @@ void main()
{
try
{
if (exists(dataFile))
{
remove(dataFile);
}
if (exists(DATA_FILE))
remove(DATA_FILE);
prepareDataFile();
walkDirs(dataPath);
walkDirs(DATA_PATH);
}
catch (Exception e)
{
import std.stdio : writeln;

writeln("Error: ", e.msg);
return;
}
Expand Down

0 comments on commit 6f8be0a

Please sign in to comment.