add login to shell

This commit is contained in:
2025-07-29 06:14:33 -05:00
parent 5333f05762
commit 8bb45eae15
10 changed files with 522 additions and 401 deletions

View File

@@ -32,6 +32,7 @@ func Pages.Index(ctx)
<a href="./api">API</a><br>
<a href={$"{repo}{commit}"}>Commit {commit.Remove(8)}</a>
<p>This website does not use cookies, unless you log in.</p>
</div>
<div class={"col"}>

View File

@@ -0,0 +1,40 @@
func Tesses.CrossLang.Shell.AddDependency(dd)
{
//crosslang add-dependency Tesses.CrossLang.Markup --version=1.0.0.0-prod
if(dd.Arguments.Length > 1)
{
var name = dd.Arguments[1];
var version = null;
each(var opt : dd.Options)
{
if(opt.Key == "version")
version = opt.Value;
}
if(!FS.Local.FileExists("cross.json"))
{
Console.WriteLine("The current directory does not have a project");
return 1;
}
if(version == null)
{
var pm = Tesses.CrossLang.PackageManager();
version = pm.GetLatest(name);
}
if(version == null)
{
Console.WriteLine("Could not get version");
return 1;
}
else
{
var data = Json.Decode(FS.ReadAllText(FS.Local,"cross.json"));
if(TypeOf(data.dependencies) != "List") data.dependencies=[];
data.dependencies.Add({name,version});
FS.WriteAllText(FS.Local,"cross.json",Json.Encode(data,true));
}
}
}

View File

@@ -0,0 +1,61 @@
func Tesses.CrossLang.Shell.AddProject(dd)
{
//crosslang add-project /path/to/project
if(dd.Arguments.Count > 1)
{
var dep = dd.Arguments[1];
if(FS.Local.DirectoryExists(dep))
{
var path = Path.FromString(dep) / "cross.json";
var pathStr = FS.MakeFull(dep).CollapseRelativeParents().ToString();
if(FS.Local.FileExists(path))
{
if(FS.Local.FileExists("cross.json"))
{
var f = FS.ReadAllText(FS.Local,"cross.json");
var json = Json.Decode(f);
if(TypeOf(json.project_dependencies) == "List")
{
each(var item : json.project_dependencies)
{
var _path = FS.MakeFull(item).CollapseRelativeParents().ToString();
if(_path == pathStr)
{
Console.WriteLine($"The project {dep} already exists in cross.json.");
return 0;
}
}
json.project_dependencies.Add(dep);
}
else
{
json.project_dependencies = [dep];
}
Console.WriteLine($"Added project {dep} to cross.json.");
FS.WriteAllText(FS.Local, "cross.json", Json.Encode(json,true));
return 0;
}
else
{
Console.WriteLine("The current directory does not have a project");
return 1;
}
}
else
{
Console.WriteLine($"The project file {path} does not exist");
return 1;
}
}
else
{
Console.WriteLine("The project directory does not exist");
return 1;
}
}
return 1;
}

View File

@@ -0,0 +1,5 @@
func Tesses.CrossLang.Shell.ConfigDir(dd)
{
Console.WriteLine(Env.CrossLangConfig);
return 0;
}

View File

@@ -0,0 +1,128 @@
func Tesses.CrossLang.Shell.InstallTool(dd)
{
//crosslang install-tool Tesses.CrossLang.Tool.SomeTool --version=1.0.0.0-prod
var toolsDir = Env.CrossLangConfig / "Tools";
var offline=false;
var buildPath = ".";
var nobuild=false;
var output=.;
each(var flag : dd.Flags)
{
if(flag == "offline")
{
offline = true;
}
else if(flag == "help")
{
Console.WriteLine("USAGE: crosslang install-tool [flags-and-options]");
Console.WriteLine("USAGE: crosslang install-tool [PackageName] [flags-and-options]");
Console.WriteLine("FLAGS:");
Console.WriteLine("offline: build with no internet (don't fetch files)");
Console.WriteLine("help: this help");
Console.WriteLine("nobuild: don't build, just install (irrelevant if you specify a PackageName)");
Console.WriteLine();
Console.WriteLine("OPTIONS:");
Console.WriteLine("conf=CONFIGSTRING: specify a conf string for compile_tool(s), is the property Config (irrelevant if you specify a PackageName)");
Console.WriteLine("version=1.0.0.0-prod: specify the package version (irrelevant if you don't specify a PackageName)");
Console.WriteLine();
Console.WriteLine("ARGUMENTS:");
Console.WriteLine("PackageName: the package name of a tool you want to download and install, if not specified we install the tool from the current directory");
return 0;
}
else if(flag == "nobuild")
{
nobuild=true;
}
}
if(dd.Arguments.Length == 1)
{
var conf = "";
each(var option : dd.Options)
{
if(option.Key == "conf")
{
conf = option.Value;
}
}
if(FS.Local.FileExists("cross.json"))
{
var proj = Json.Decode(FS.ReadAllText(FS.Local,"cross.json"));
var name = TypeOf(proj.info.toolname) == "String" ? proj.info.toolname : proj.name;
if(proj.info.type != "tool")
{
Console.WriteLine("The project is not a tool");
return 1;
}
if(TypeOf(name) != "String")
{
Console.WriteLine("The tool does not have a name");
return 1;
}
var toolDir = toolsDir / name;
FS.Local.CreateDirectory(toolDir);
var toolFile = toolDir / name + ".crvm";
if(nobuild)
{
var nameVer = $"{proj.name}-{proj.version}.crvm";
var buildDir = TypeOf(proj.bin_directory) == "String" ? proj.bin_directory : "bin";
output = ./buildDir/nameVer;
if(!FS.Local.FileExists(output))
{
Console.WriteLine($"ERROR: file {output} does not exist.");
return 1;
}
}
else
{
var pm = Tesses.CrossLang.PackageManager();
pm.Offline = offline;
var bt = Tesses.CrossLang.BuildTool(pm);
bt.Config = conf;
output = bt.BuildProject(buildPath).Output;
}
func CopyCRVM(src,dest)
{
func copyFile(src,dest)
{
var _src = FS.Local.OpenFile(src,"rb");
var _dest = FS.Local.OpenFile(dest, "wb");
_src.CopyTo(_dest);
_src.Close();
_dest.Close();
Console.WriteLine($"{src} -> {dest}");
}
if(FS.Local.FileExists(dest)) return;
copyFile(src,dest);
var srcStrm = FS.Local.OpenFile(src,"rb");
var crvm = VM.LoadExecutable(srcStrm);
srcStrm.Close();
each(var dep : crvm.Dependencies)
{
var name = $"{dep.Name}-{dep.Version.ToString()}.crvm";
CopyCRVM(src.GetParent()/name, dest.GetParent()/name);
}
}
CopyCRVM(output,toolFile);
return 0;
}
else
{
Console.WriteLine("The current directory does not have a project");
return 1;
}
}
return 0;
}

View File

@@ -0,0 +1,117 @@
func Tesses.CrossLang.Shell.Login(dd)
{
var accounts = [];
if(FS.Local.FileExists(Env.CrossLangConfig / "auth.json"))
{
accounts = Json.Decode(FS.ReadAllText(FS.Local,Env.CrossLangConfig / "auth.json"));
if(TypeOf(accounts) != "List") accounts = [];
}
//crosslang login local https://cpkg.tesseslanguage.com/
//crosslang login [NAME] [HOST] [TOKEN]
if(dd.Arguments.Length < 3)
{
//not valid
}
else if(dd.Arguments.Length == 3)
{
//name server-url
var name = dd.Arguments[1];
var host = dd.Arguments[2];
Console.Write("Email: ");
var email = Console.ReadLine();
Console.Write("Password: ");
var echo = Console.Echo;
Console.Echo = false;
var password = Console.ReadLine();
Console.Echo = echo;
Console.WriteLine();
var accountRequest = {
email,
password
};
var req = {
Method="POST",
Body = Net.Http.TextHttpRequestBody(accountRequest.ToString(),"application/json")
};
var http = Net.Http.MakeRequest($"{host.TrimEnd('/')}/api/v1/login",req);
if(http.StatusCode == 401 && http.ResponseHeaders.TryGetFirst("Content-Type") == "application/json")
{
var json = Json.Decode(http.ReadAsString());
Console.WriteLined($"Failed to login, reason={json.reason}");
return 1;
}
else if(http.ResponseHeaders.TryGetFirst("Content-Type") == "application/json")
{
var json = Json.Decode(http.ReadAsString());
var hasSet=false;
each(var ents : accounts)
{
if(ents.name == name)
{
ents.host = host;
ents.token = json.token;
hasSet=true;
}
}
if(!hasSet)
{
accounts.Add({
name,
host,
token = json.token
});
}
FS.WriteAllText(FS.Local,Env.CrossLangConfig / "auth.json",Json.Encode(accounts));
Console.WriteLine("Logged in successfully");
}
}
else if(dd.Arguments.Length == 4)
{
var name = dd.Arguments[1];
var host = dd.Arguments[2];
var token = dd.Arguments[3];
var hasSet=false;
each(var ents : accounts)
{
if(ents.name == name)
{
ents.host = host;
ents.token = token;
hasSet=true;
}
}
if(!hasSet)
{
accounts.Add({
name,
host,
token
});
}
FS.WriteAllText(FS.Local,Env.CrossLangConfig / "auth.json",Json.Encode(accounts));
Console.WriteLine("Added session successfully");
}
}

View File

@@ -15,410 +15,13 @@
if(commandName == "install-tool")
{
//crosslang install-tool Tesses.CrossLang.Tool.SomeTool --version=1.0.0.0-prod
var toolsDir = Env.CrossLangConfig / "Tools";
var offline=false;
var buildPath = ".";
var nobuild=false;
var output=.;
each(var flag : dd.Flags)
{
if(flag == "offline")
{
offline = true;
}
else if(flag == "help")
{
Console.WriteLine("USAGE: crosslang install-tool [flags-and-options]");
Console.WriteLine("USAGE: crosslang install-tool [PackageName] [flags-and-options]");
Console.WriteLine("FLAGS:");
Console.WriteLine("offline: build with no internet (don't fetch files)");
Console.WriteLine("help: this help");
Console.WriteLine("nobuild: don't build, just install (irrelevant if you specify a PackageName)");
Console.WriteLine();
Console.WriteLine("OPTIONS:");
Console.WriteLine("conf=CONFIGSTRING: specify a conf string for compile_tool(s), is the property Config (irrelevant if you specify a PackageName)");
Console.WriteLine("version=1.0.0.0-prod: specify the package version (irrelevant if you don't specify a PackageName)");
Console.WriteLine();
Console.WriteLine("ARGUMENTS:");
Console.WriteLine("PackageName: the package name of a tool you want to download and install, if not specified we install the tool from the current directory");
return 0;
}
else if(flag == "nobuild")
{
nobuild=true;
}
}
if(dd.Arguments.Length == 1)
{
var conf = "";
each(var option : dd.Options)
{
if(option.Key == "conf")
{
conf = option.Value;
}
}
if(FS.Local.FileExists("cross.json"))
{
var proj = Json.Decode(FS.ReadAllText(FS.Local,"cross.json"));
var name = TypeOf(proj.info.toolname) == "String" ? proj.info.toolname : proj.name;
if(proj.info.type != "tool")
{
Console.WriteLine("The project is not a tool");
return 1;
}
if(TypeOf(name) != "String")
{
Console.WriteLine("The tool does not have a name");
return 1;
}
var toolDir = toolsDir / name;
FS.Local.CreateDirectory(toolDir);
var toolFile = toolDir / name + ".crvm";
if(nobuild)
{
var nameVer = $"{proj.name}-{proj.version}.crvm";
var buildDir = TypeOf(proj.bin_directory) == "String" ? proj.bin_directory : "bin";
output = ./buildDir/nameVer;
if(!FS.Local.FileExists(output))
{
Console.WriteLine($"ERROR: file {output} does not exist.");
return 1;
}
}
else
{
var pm = Tesses.CrossLang.PackageManager();
pm.Offline = offline;
var bt = Tesses.CrossLang.BuildTool(pm);
bt.Config = conf;
output = bt.BuildProject(buildPath).Output;
}
func CopyCRVM(src,dest)
{
func copyFile(src,dest)
{
var _src = FS.Local.OpenFile(src,"rb");
var _dest = FS.Local.OpenFile(dest, "wb");
_src.CopyTo(_dest);
_src.Close();
_dest.Close();
Console.WriteLine($"{src} -> {dest}");
}
if(FS.Local.FileExists(dest)) return;
copyFile(src,dest);
var srcStrm = FS.Local.OpenFile(src,"rb");
var crvm = VM.LoadExecutable(srcStrm);
srcStrm.Close();
each(var dep : crvm.Dependencies)
{
var name = $"{dep.Name}-{dep.Version.ToString()}.crvm";
CopyCRVM(src.GetParent()/name, dest.GetParent()/name);
}
}
CopyCRVM(output,toolFile);
return 0;
}
else
{
Console.WriteLine("The current directory does not have a project");
return 1;
}
}
}
else if(commandName == "console")
{
//crosslang console myfavoriteapp
}
else if(commandName == "tool")
{
var dir = Env.CrossLangConfig / "Tools";
if(dd.Arguments.Length == 1)
{
Console.WriteLine("List of tools:");
each(var item : FS.Local.EnumeratePaths(dir))
{
Console.WriteLine(Tesses.CrossLang.GetNameAndDescription(item / item.GetFileName() + ".crvm"));
}
return 0;
}
}
else if(commandName == "tool-test")
{
var pm = Tesses.CrossLang.PackageManager();
pm.Offline = false;
var bt = Tesses.CrossLang.BuildTool(pm);
var proj=bt.BuildProject(".");
var output = proj.Output;
var env = VM.CreateEnvironment({});
env.RegisterEverything();
env.LockRegister();
env.LoadFileWithDependencies(FS.Local,output);
var myArgs = [];
for(var i = 1; i < dd.Arguments.Count; i++)
{
myArgs.Add(dd.Arguments[i]);
}
return env.GetDictionary().RunTool({
Arguments=myArgs,
Options = dd.Options,
Flags = dd.Flags,
ToolName = proj.Info.toolname
});
}
else if(commandName == "add-project")
{
//crosslang add-project /path/to/project
if(dd.Arguments.Count > 1)
{
var dep = dd.Arguments[1];
if(FS.Local.DirectoryExists(dep))
{
var path = Path.FromString(dep) / "cross.json";
var pathStr = FS.MakeFull(dep).CollapseRelativeParents().ToString();
if(FS.Local.FileExists(path))
{
if(FS.Local.FileExists("cross.json"))
{
var f = FS.ReadAllText(FS.Local,"cross.json");
var json = Json.Decode(f);
if(TypeOf(json.project_dependencies) == "List")
{
each(var item : json.project_dependencies)
{
var _path = FS.MakeFull(item).CollapseRelativeParents().ToString();
if(_path == pathStr)
{
Console.WriteLine($"The project {dep} already exists in cross.json.");
return 0;
}
}
json.project_dependencies.Add(dep);
}
else
{
json.project_dependencies = [dep];
}
Console.WriteLine($"Added project {dep} to cross.json.");
FS.WriteAllText(FS.Local, "cross.json", Json.Encode(json,true));
return 0;
}
else
{
Console.WriteLine("The current directory does not have a project");
return 1;
}
}
else
{
Console.WriteLine($"The project file {path} does not exist");
return 1;
}
}
else
{
Console.WriteLine("The project directory does not exist");
return 1;
}
}
return 1;
}
else if(commandName == "add-dependency")
{
//crosslang add-dependency Tesses.CrossLang.Markup --version=1.0.0.0-prod
if(dd.Arguments.Length > 1)
{
var name = dd.Arguments[1];
var version = null;
each(var opt : dd.Options)
{
if(opt.Key == "version")
version = opt.Value;
}
if(!FS.Local.FileExists("cross.json"))
{
Console.WriteLine("The current directory does not have a project");
return 1;
}
if(version == null)
{
var pm = Tesses.CrossLang.PackageManager();
version = pm.GetLatest(name);
}
if(version == null)
{
Console.WriteLine("Could not get version");
return 1;
}
else
{
var data = Json.Decode(FS.ReadAllText(FS.Local,"cross.json"));
if(TypeOf(data.dependencies) != "List") data.dependencies=[];
data.dependencies.Add({name,version});
FS.WriteAllText(FS.Local,"cross.json",Json.Encode(data,true));
}
}
}
else if(commandName == "upload-package")
{
//crosslang upload-package [--host=HOST --token=TOKEN] [--session=NAME] [PACKAGE_FILE]
var host=null;
var token=null;
var session=null;
var package=null;
var nobuild=false;
each(var option : dd.Options)
{
if(option.Key == "host")
{
host = option.Value;
}
if(option.Key == "token")
{
token = option.Value;
}
if(option.Key == "session")
{
session = option.Value;
}
}
if(dd.Arguments.Length > 1)
{
package = dd.Arguments[1];
}
else {
if(nobuild)
{
throw {Type="NotImplementedException", Message="nobuild on upload-package is not implemented",ToString=(this)=>$"{this.Type} on line: {this.Line}: {this.Message}"};
}
else {
var pm = Tesses.CrossLang.PackageManager();
pm.Offline = false;
var bt = Tesses.CrossLang.BuildTool(pm);
bt.Config = "";
bt.AllowFullCompTime = false;
package = bt.BuildProject(".").Output;
}
}
if(TypeOf(host) != "String" || TypeOf(token) != "String")
{
if(FS.Local.FileExists(Env.CrossLangConfig / "auth.json"))
{
var json = Json.Decode(FS.ReadAllText(FS.Local,Env.CrossLangConfig / "auth.json"));
if(json.Length == 0)
{
Console.WriteLine("The auth.json file is empty, use crosslang login to login");
return 1;
}
else if(json.Length == 1)
{
host = json[0].host;
token = json[0].token;
}
else {
if(TypeOf(session) != "String")
{
Console.WriteLine("Multiple entries in auth.json file, session is ambiguous.");
Console.WriteLine("Sessions:");
each(var item : json)
{
Console.WriteLine($"{item.name}: {item.host}");
}
return 1;
}
else {
var found=false;
each(var item : json)
{
if(item.name == session)
{
found=true;
host = item.host;
token = item.token;
break;
}
}
if(!found) {
Console.WriteLine($"Could not find session with name: {session}");
return 1;
}
}
}
}
else { Console.WriteLine("No auth.json file, use crosslang login to login"); return 1;}
}
if(TypeOf(host) != "String" || TypeOf(token) != "String")
{
Console.WriteLine("You are not logged in, use crosslang login to login");
return 1;
}
var strm = FS.Local.OpenFile(package,"rb");
if(strm == null)
{
Console.WriteLine("Could not open file");
return 1;
}
var req = {
Method="PUT",
RequestHeaders = [
{Key= "Authorization",Value=$"Bearer {token}"}
],
Body = Net.Http.StreamHttpRequestBody(strm,"application/crvm")
};
var http = Net.Http.MakeRequest($"{host.TrimEnd('/')}/api/v1/upload",req);
if(http.StatusCode == 204)
{
Console.WriteLine("Uploaded package successfully");
}
else if(http.ResponseHeaders.TryGetFirst("Content-Type") == "application/json")
{
var json = Json.Decode(http.ReadAsString());
Console.WriteLine($"Failed to upload package, {json.reason.TrimEnd('.')}.");
}
else {
Console.WriteLine($"Failed to upload package.");
}
strm.Close();
}
else if(commandName == "login")
{
//crosslang login local https://cpkg.tesseslanguage.com/
//crosslang login [NAME] [HOST] [TOKEN]
}
else if(commandName == "configdir")
{
Console.WriteLine(Env.CrossLangConfig);
}

View File

@@ -0,0 +1,27 @@
func Tesses.CrossLang.Shell.ToolTest(dd)
{
var pm = Tesses.CrossLang.PackageManager();
pm.Offline = false;
var bt = Tesses.CrossLang.BuildTool(pm);
var proj=bt.BuildProject(".");
var output = proj.Output;
var env = VM.CreateEnvironment({});
env.RegisterEverything();
env.LockRegister();
env.LoadFileWithDependencies(FS.Local,output);
var myArgs = [];
for(var i = 1; i < dd.Arguments.Count; i++)
{
myArgs.Add(dd.Arguments[i]);
}
return env.GetDictionary().RunTool({
Arguments=myArgs,
Options = dd.Options,
Flags = dd.Flags,
ToolName = proj.Info.toolname
});
}

View File

@@ -0,0 +1,13 @@
func Tesses.CrossLang.Shell.Tool(dd)
{
var dir = Env.CrossLangConfig / "Tools";
if(dd.Arguments.Length == 1)
{
Console.WriteLine("List of tools:");
each(var item : FS.Local.EnumeratePaths(dir))
{
Console.WriteLine(Tesses.CrossLang.GetNameAndDescription(item / item.GetFileName() + ".crvm"));
}
return 0;
}
}

View File

@@ -0,0 +1,126 @@
func Tesses.CrossLang.Shell.UploadPackage(dd)
{
//crosslang upload-package [--host=HOST --token=TOKEN] [--session=NAME] [PACKAGE_FILE]
var host=null;
var token=null;
var session=null;
var package=null;
var nobuild=false;
each(var option : dd.Options)
{
if(option.Key == "host")
{
host = option.Value;
}
if(option.Key == "token")
{
token = option.Value;
}
if(option.Key == "session")
{
session = option.Value;
}
}
if(dd.Arguments.Length > 1)
{
package = dd.Arguments[1];
}
else {
if(nobuild)
{
throw {Type="NotImplementedException", Message="nobuild on upload-package is not implemented",ToString=(this)=>$"{this.Type} on line: {this.Line}: {this.Message}"};
}
else {
var pm = Tesses.CrossLang.PackageManager();
pm.Offline = false;
var bt = Tesses.CrossLang.BuildTool(pm);
bt.Config = "";
bt.AllowFullCompTime = false;
package = bt.BuildProject(".").Output;
}
}
if(TypeOf(host) != "String" || TypeOf(token) != "String")
{
if(FS.Local.FileExists(Env.CrossLangConfig / "auth.json"))
{
var json = Json.Decode(FS.ReadAllText(FS.Local,Env.CrossLangConfig / "auth.json"));
if(json.Length == 0)
{
Console.WriteLine("The auth.json file is empty, use crosslang login to login");
return 1;
}
else if(json.Length == 1)
{
host = json[0].host;
token = json[0].token;
}
else {
if(TypeOf(session) != "String")
{
Console.WriteLine("Multiple entries in auth.json file, session is ambiguous.");
Console.WriteLine("Sessions:");
each(var item : json)
{
Console.WriteLine($"{item.name}: {item.host}");
}
return 1;
}
else {
var found=false;
each(var item : json)
{
if(item.name == session)
{
found=true;
host = item.host;
token = item.token;
break;
}
}
if(!found) {
Console.WriteLine($"Could not find session with name: {session}");
return 1;
}
}
}
}
else { Console.WriteLine("No auth.json file, use crosslang login to login"); return 1;}
}
if(TypeOf(host) != "String" || TypeOf(token) != "String")
{
Console.WriteLine("You are not logged in, use crosslang login to login");
return 1;
}
var strm = FS.Local.OpenFile(package,"rb");
if(strm == null)
{
Console.WriteLine("Could not open file");
return 1;
}
var req = {
Method="PUT",
RequestHeaders = [
{Key= "Authorization",Value=$"Bearer {token}"}
],
Body = Net.Http.StreamHttpRequestBody(strm,"application/crvm")
};
var http = Net.Http.MakeRequest($"{host.TrimEnd('/')}/api/v1/upload",req);
if(http.StatusCode == 204)
{
Console.WriteLine("Uploaded package successfully");
}
else if(http.ResponseHeaders.TryGetFirst("Content-Type") == "application/json")
{
var json = Json.Decode(http.ReadAsString());
Console.WriteLine($"Failed to upload package, {json.reason.TrimEnd('.')}.");
}
else {
Console.WriteLine($"Failed to upload package.");
}
strm.Close();
}