Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generate Index.js #213

Merged
merged 4 commits into from
Sep 26, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions ARCtrl.sln
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "playground", "playground",
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "JavaScript", "JavaScript", "{913222CA-261F-49CB-A823-CC7C335F964A}"
ProjectSection(SolutionItems) = preProject
tests\JavaScript\ARCtrl.Index.js = tests\JavaScript\ARCtrl.Index.js
tests\JavaScript\ISA.CompositeHeader.js = tests\JavaScript\ISA.CompositeHeader.js
tests\JavaScript\ISA.Person.js = tests\JavaScript\ISA.Person.js
tests\JavaScript\Main.js = tests\JavaScript\Main.js
Expand Down
1 change: 1 addition & 0 deletions build/Build.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
<Compile Include="ProjectInfo.fs" />
<Compile Include="ReleaseNotesTasks.fs" />
<Compile Include="BasicTasks.fs" />
<Compile Include="GenerateIndexJs.fs" />
<Compile Include="TestTasks.fs" />
<Compile Include="PackageTasks.fs" />
<Compile Include="ReleaseTasks.fs" />
Expand Down
98 changes: 98 additions & 0 deletions build/GenerateIndexJs.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
module GenerateIndexJs

open System
open System.IO
open System.Text.RegularExpressions

let private getAllJsFiles(path: string) =
let options = EnumerationOptions()
options.RecurseSubdirectories <- true
IO.Directory.EnumerateFiles(path,"*.js",options)

let private pattern (className: string) = sprintf @"^export class (?<ClassName>%s)+[\s{].*({)?" className

type private FileInformation = {
FilePath : string
Lines : string []
} with
static member create(filePath: string, lines: string []) = {
FilePath = filePath
Lines = lines
}

let private findClasses (rootPath: string) (cois: string []) (filePaths: seq<string> ) =
let files = [|
for fp in filePaths do
yield FileInformation.create(fp, System.IO.File.ReadAllLines (fp))
|]
let importStatements = ResizeArray()
let findClass (className: string) =
/// maybe set this as default if you do not want any whitelist
let classNameDefault = @"[a-zA-Z_0-9]"
let regex = Regex(Regex.Escape(className) |> pattern)
let mutable found = false
let mutable result = None
let enum = files.GetEnumerator()
while not found && enum.MoveNext() do
let fileInfo = enum.Current :?> FileInformation
for line in fileInfo.Lines do
let m = regex.Match(line)
match m.Success with
| true ->
found <- true
result <- Some <| (className, IO.Path.GetRelativePath(rootPath,fileInfo.FilePath))
| false ->
()
match result with
| None ->
failwithf "Unable to find %s" className
| Some r ->
importStatements.Add r
for coi in cois do findClass coi
importStatements
|> Array.ofSeq

open System.Text

let private createImportStatements (info: (string*string) []) =
let sb = StringBuilder()
let importCollection = info |> Array.groupBy snd |> Array.map (fun (p,a) -> p, a |> Array.map fst )
for filePath, imports in importCollection do
let p = filePath.Replace("\\","/")
sb.Append "export { " |> ignore
sb.AppendJoin(", ", imports) |> ignore
sb.Append " } from " |> ignore
sb.Append (sprintf "\"./%s\"" p) |> ignore
sb.Append ";" |> ignore
sb.AppendLine() |> ignore
sb.ToString()

let private writeJsIndexfile (path: string) (fileName: string) (content: string) =
let filePath = Path.Combine(path, fileName)
File.WriteAllText(filePath, content)

let private generateIndexfile (rootPath: string, fileName: string, whiteList: string []) =
getAllJsFiles(rootPath)
|> findClasses rootPath whiteList
|> createImportStatements
|> writeJsIndexfile rootPath fileName

let ARCtrl_generate(rootPath: string) =
let whiteList = [|
Freymaurer marked this conversation as resolved.
Show resolved Hide resolved
"Comment$"
"Person"
"OntologyAnnotation";
"IOType"
"CompositeHeader";
"CompositeCell"
"CompositeColumn"
"ArcTable"
"ArcAssay";
"ArcStudy";
"ArcInvestigation";
"Template"
"Organisation"
"JsWeb"
"ARC"
|]
generateIndexfile(rootPath, "index.js", whiteList)
18 changes: 11 additions & 7 deletions build/TestTasks.fs
Original file line number Diff line number Diff line change
Expand Up @@ -114,21 +114,25 @@ module private Helper =
processes
|> Proc.Parallel.run
|> ignore

module RunTests =

let runTestsJsNative = BuildTask.create "runTestsJSNative" [clean; build] {
Trace.traceImportant "Start native JavaScript tests"
for path in ProjectInfo.jsTestProjects do
// transpile library for native access
run dotnet $"fable src/ARCtrl -o {path}/ARCtrl" ""
GenerateIndexJs.ARCtrl_generate($"{path}/ARCtrl")
run npx $"mocha {path} --timeout 20000" ""
}

let runTestsJs = BuildTask.create "runTestsJS" [clean; build] {
for path in ProjectInfo.testProjects do
// transpile js files from fsharp code
run dotnet $"fable {path} -o {path}/js" ""
// run mocha in target path to execute tests
// "--timeout 20000" is used, because json schema validation takes a bit of time.
run npx $"mocha {path}/js --timeout 20000" ""
Trace.traceImportant "Start native JavaScript tests"
for path in ProjectInfo.jsTestProjects do
// transpile library for native access
run dotnet $"fable src/ARCtrl -o {path}/ARCtrl" ""
run npx $"mocha {path} --timeout 20000" ""
}

let runTestsDotnet = BuildTask.create "runTestsDotnet" [clean; build] {
Expand All @@ -145,6 +149,6 @@ module RunTests =
)
}

let runTests = BuildTask.create "RunTests" [clean; build; RunTests.runTestsJs; RunTests.runTestsDotnet] {
let runTests = BuildTask.create "RunTests" [clean; build; RunTests.runTestsJs; RunTests.runTestsJsNative; RunTests.runTestsDotnet] {
()
}
4 changes: 3 additions & 1 deletion build/release_package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"version": "1.0.0+1c6c4fa",
"description": "Top level ARC DataModel and API function descriptions.",
"type": "module",
"main": "Index.js",
"repository": {
"type": "git",
"url": "git+https://github.com/nfdi4plants/arcAPI.git"
Expand All @@ -17,6 +18,7 @@
},
"homepage": "https://github.com/nfdi4plants/arcAPI#readme",
"dependencies": {
"fable-library": "^1.1.1"
"fable-library": "^1.1.1",
"isomorphic-fetch": "^3.0.0"
}
}
31 changes: 12 additions & 19 deletions src/ARCtrl/ARCtrl.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,6 @@
<TargetFramework>netstandard2.0</TargetFramework>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
</PropertyGroup>
<PropertyGroup>
<Authors>nfdi4plants, Lukas Weil, Kevin Frey, Kevin Schneider, Oliver Maus</Authors>
<Description>Library for management of Annotated Research Contexts (ARCs) using an in-memory representation and runtimer agnostic contract systems.</Description>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<PackageIcon>logo.png</PackageIcon>
<PackageTags>ARC F# FSharp dotnet .Net bioinformatics biology fable-library datascience dataplant nfdi metadata</PackageTags>
<PackageProjectUrl>https://github.com/nfdi4plants/ARCtrl</PackageProjectUrl>
<RepositoryUrl>https://github.com/nfdi4plants/ARCtrl</RepositoryUrl>
<RepositoryType>git</RepositoryType>
</PropertyGroup>
<ItemGroup>
<None Include="../../build/logo.png" Pack="true" PackagePath="\" />
</ItemGroup>
<ItemGroup>
<None Include="paket.references" />
<Compile Include="Path.fs" />
Expand All @@ -43,13 +30,19 @@
<ProjectReference Include="..\ISA\ISA.Json\ARCtrl.ISA.Json.fsproj" />
<ProjectReference Include="..\ISA\ISA.Spreadsheet\ARCtrl.ISA.Spreadsheet.fsproj" />
</ItemGroup>
<PropertyGroup>
<NpmDependencies>
<NpmPackage Name="isomorphic-fetch" Version="gt 3.0.0 lt 3.0.0" ResolutionStrategy="Max" />
</NpmDependencies>
</PropertyGroup>
<ItemGroup>
<Content Include="*.fsproj; **\*.fs; **\*.fsi" PackagePath="fable\" />
<None Include="../../build/logo.png" Pack="true" PackagePath="\" />
<Compile Include="Index.fs" />
</ItemGroup>
<PropertyGroup>
<Authors>nfdi4plants, Lukas Weil, Kevin Frey, Kevin Schneider, Oliver Maus</Authors>
<Description>Library for management of Annotated Research Contexts (ARCs) using an in-memory representation and runtimer agnostic contract systems.</Description>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<PackageIcon>logo.png</PackageIcon>
<PackageTags>ARC F# FSharp dotnet .Net bioinformatics biology fable-library datascience dataplant nfdi metadata</PackageTags>
<PackageProjectUrl>https://github.com/nfdi4plants/ARCtrl</PackageProjectUrl>
<RepositoryUrl>https://github.com/nfdi4plants/ARCtrl</RepositoryUrl>
<RepositoryType>git</RepositoryType>
</PropertyGroup>
<Import Project="..\..\.paket\Paket.Restore.targets" />
</Project>
10 changes: 10 additions & 0 deletions src/ARCtrl/Index.fs
Freymaurer marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module ARCtrl.Index

open Fable.Core
open Fable.Core.JsInterop

exportDefault {|
ArcAssay = ARCtrl.ISA.ArcAssay
ArcStudy = ARCtrl.ISA.ArcStudy
ArcInvestigation = ARCtrl.ISA.ArcInvestigation
|}
2 changes: 0 additions & 2 deletions src/ARCtrl/Templates/Template.Web.fs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ let getTemplates(url: string option) =
return mapResult
}

open Fable.Core.JsInterop

/// <summary>
/// This class is used to make async functions more accessible from JavaScript.
/// </summary>
Expand Down
20 changes: 20 additions & 0 deletions tests/JavaScript/ARCtrl.Index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { equal, deepEqual } from 'assert';
import { OntologyAnnotation, ArcAssay, ArcInvestigation, ArcTable } from './ARCtrl/index.js'

//import file './ARCtrl/index.js' is generated by FAKE never by NPM.
describe('Auto Generated Index', function () {
it('OntologyAnnotation', function () {
let actual = OntologyAnnotation.fromString("My OA")
equal(actual.NameText, "My OA")
});
it('ArcAssay', function () {
let actual = new ArcAssay("My Assay", null, null, null, [ArcTable.init("MyTable")])
equal(actual.Identifier, "My Assay", "Id")
equal(actual.TableCount, 1, "TableCount")
});
it('ArcInvestigation', function () {
let actual = new ArcInvestigation("My Investigation", "Super Awesome Experiment")
equal(actual.Identifier, "My Investigation", "Id")
equal(actual.Title, "Super Awesome Experiment", "Title")
});
});