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

Prod 336/nested path dep #102

Merged
merged 6 commits into from
Aug 9, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
488 changes: 346 additions & 142 deletions src/dependencies.stanza

Large diffs are not rendered by default.

36 changes: 27 additions & 9 deletions src/dependency.stanza
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ public defmulti name (d: Dependency) -> String
public defmulti path (d: Dependency) -> String
public defmulti version-string? (d: Dependency) -> Maybe<String>:
None()
public defmulti dependency-type-name (d:Dependency) -> String

doc: \<DOC>
Indicates whether a dependency should be parsed recursively.

Expand All @@ -26,26 +28,42 @@ dependencies with.
Other deps, like the `TaskDependency` does not have an `slm.toml`
and is primarily for executing a command/process for external
initialization.

This flag is for differentiating between these two types of
dependency.
<DOC>
public defmulti recursive (d:Dependency) -> True|False :
public deftype RecursiveDependency <: Dependency
public defmulti recursive (d:Dependency) -> True|False
defmethod recursive (d:Dependency) -> True|False :
false
defmethod recursive (d:RecursiveDependency) -> True|False :
true

doc: \<DOC>
Tool for filtering dependencies for recursives only.
<DOC>
public defn recursive-deps-only (deps:Seqable<Dependency>) -> Seqable<Dependency> :
for dep in deps filter:
recursive(dep)
public defn recursive-deps-only (deps:Seqable<Dependency>) -> Seqable<RecursiveDependency> :
filter-by<RecursiveDependency>(deps)

public defstruct UnknownDependencyTypeError <: Exception :
dep:Dependency

public defmethod print (o:OutputStream, e:UnknownDependencyTypeError):
val msg = "Unknown and Unhandled Dependency Type: %_" % [dep(e)]
print(o, msg)
print(o, "Unexpected dependency type: %_" % [object-type(dep(e))])

;============================================================
;===================== Terminal Colors ======================
;============================================================

public defn colored-name? (name:String) -> ColoredString :
ColoredString(name)
$> bold $> foreground{_, TerminalBrightWhite}
$> clear-color?
public defn colored-name? (d:Dependency) -> ColoredString :
colored-name?(name(d))

public defn colored-version? (version:String) -> ColoredString :
ColoredString(version)
$> bold $> foreground{_, TerminalBrightGreen}
$> clear-color?
public defn colored-version? (v:SemanticVersion) -> ColoredString :
colored-version?(to-string(v))
public defn colored-version? (d:Dependency) -> ColoredString :
colored-version?(value-or(version-string?(d), ""))
15 changes: 4 additions & 11 deletions src/git-dep.stanza
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ defpackage slm/git-dep:
import slm/libgit-utils

; Dependencies specified by Git locator/version (e.g. `foo = "myorg/myuser|1.0.0"`)
public defstruct GitDependency <: Dependency:
public defstruct GitDependency <: RecursiveDependency:
name: String with: (as-method => true)
locator: String
version: SemanticVersion
Expand All @@ -26,22 +26,15 @@ defmethod path (d: GitDependency):
defmethod version-string? (d: GitDependency) -> One<String>:
One(version-string(d))

defmethod dependency-type-name (d:GitDependency) -> String :
"git"

defmethod print (o:OutputStream, d:GitDependency) :
print(o, "%_ = { git = \"%_\", version = \"%_\" }" % [name(d), locator(d), version-string(d)])

public defn version-string (d: GitDependency) -> String:
to-string(version(d))

public defn colored-version? (d: GitDependency) -> ColoredString:
ColoredString(version-string(d))
$> bold $> foreground{_, TerminalBrightGreen}
$> clear-color?

public defn colored-name? (d: Dependency) -> ColoredString:
ColoredString(name(d))
$> bold $> foreground{_, TerminalBrightWhite}
$> clear-color?

doc: \<DOC>
Legacy Version of the Git Dependency Parser

Expand Down
20 changes: 12 additions & 8 deletions src/logging.stanza
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,12 @@ public defn program-name () -> String:
val program-path = command-line-arguments()[0]
program-path $> base-name? $> value-or{_, program-path}

defn colored-program-prefix () -> ColoredString:
val prefix = to-string("%_:" % [program-name()])
ColoredString(prefix, ColorSpec() $> bold $> foreground{_, TerminalBrightWhite})

defn program-prefix () -> ColoredString|String:
val prefix = to-string("%_:" % [program-name()])
if supports-color?():
colored-program-prefix()
ColoredString(prefix, ColorSpec() $> bold $> foreground{_, TerminalBrightWhite})
else:
to-string("%_:" % [program-name()])
prefix

defn sub-command () -> String|Printable :
val args = command-line-arguments()
Expand All @@ -45,8 +42,16 @@ public defn info (msg: Printable|String) -> False:
println(current-output-stream(), "%_ %_" % [program-prefix(), msg])
flush(current-output-stream() as FileOutputStream)

public defn warn (msg: Printable|String) -> False:
println("%_ %_%_" % [
program-prefix(),
ColoredString("warning: ")
$> bold $> dim $> foreground{_, TerminalYellow}
$> clear-color?,
msg,
])

public defn error (msg: Printable|String -- code:Int = 1) -> Void:
val program = program-name()
println(current-error-stream(), "%_ %_ %_" % [
program-prefix(),
ColoredString("error:")
Expand All @@ -55,4 +60,3 @@ public defn error (msg: Printable|String -- code:Int = 1) -> Void:
msg,
])
exit(code)

22 changes: 16 additions & 6 deletions src/path-dep.stanza
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
defpackage slm/path-dep:
import core
import core/parsed-path

import semver
import maybe-utils
Expand All @@ -10,7 +11,7 @@ defpackage slm/path-dep:


; Dependencies specified by path (e.g. `foo = { path = "../foo" }`)
public defstruct PathDependency <: Dependency:
public defstruct PathDependency <: RecursiveDependency:
name: String with: (as-method => true)
path: String with: (as-method => true)
version?: Maybe<SemanticVersion> with: ( default => None() )
Expand All @@ -23,18 +24,27 @@ defmethod version-string? (d:PathDependency) -> Maybe<String>:
public defn version-string! (d:PathDependency) -> String:
to-string $ value-or(version?(d), "UNSPECIFIED")

defmethod dependency-type-name (d:PathDependency) -> String :
"path"

defmethod print (o:OutputStream, d:PathDependency) :
print(o, "%_ = { path = \"%_\"" % [name(d), path(d)])
match(version?(d)):
(x:None): print(o, " }")
(x:One<SemanticVersion>): print(o, ", version = \"%_\" }" % [to-string $ value(x)])


public defn parse-path-dependency (name: String, path: String, version?:Maybe<String>, env-sub-enable:True|False) -> PathDependency:
val path* = un-norm-path $ if env-sub-enable:
env-var-substitute(path)
else:
path
public defn parse-path-dependency (name:String, path:String,
version?:Maybe<String>,
toml-dir:String, env-sub-enable:True|False)
-> PathDependency:
val path* = let :
val sub-path = env-var-substitute(path) when env-sub-enable else path
val parsed-path = parse-path(sub-path)
;If the path is relative, prepend the toml file path to it
val anchored-path = parsed-path when absolute?(parsed-path) else
relative-to-dir(parse-path(toml-dir), parsed-path)
un-norm-path(to-string(anchored-path))

val version = match(version?):
(x:None): x
Expand Down
24 changes: 5 additions & 19 deletions src/pkg-dep.stanza
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ defpackage slm/pkg-dep:
import collections

import libarchive
import semver
import maybe-utils
import term-colors
import toml
Expand All @@ -26,7 +27,7 @@ This dependency is intended to define a pre-compiled archive
of stanza pkg binaries which can be linked into an slm project.
This archive may be stored on a remote conan/artifactory server.
<DOC>
public defstruct PkgDependency <: Dependency & Hashable & Equalable:
public defstruct PkgDependency <: RecursiveDependency & Hashable & Equalable:
name:String with: (
as-method => true
ensure => ensure-not-empty!
Expand All @@ -37,16 +38,15 @@ public defstruct PkgDependency <: Dependency & Hashable & Equalable:
with:
hashable => true

defmethod recursive (d:PkgDependency) -> True|False :
; a pkg can have recursive deps
true

defmethod path (d:PkgDependency) -> String:
path-join(SLM_DEPS_DIR, name(d))

defmethod equal? (a:PkgDependency, b:PkgDependency) -> True|False :
name(a) == name(b) and version(a) == version(b) and type(a) == type(b) and to-string(options(a)) == to-string(options(b))

defmethod dependency-type-name (d:PkgDependency) -> String :
"package"

public defn PkgDependency (
name: String
version: String ; may not always be a SemanticVersion
Expand All @@ -71,16 +71,6 @@ defmethod print (o:OutputStream, d:PkgDependency) :
public defn version-string (d: PkgDependency) -> String:
to-string(version(d))

public defn colored-version? (d: PkgDependency) -> ColoredString:
ColoredString(version-string(d))
$> bold $> foreground{_, TerminalBrightGreen}
$> clear-color?

public defn colored-name? (d: PkgDependency) -> ColoredString:
ColoredString(name(d))
$> bold $> foreground{_, TerminalBrightWhite}
$> clear-color?

defn options-to-string (d:PkgDependency) -> String :
val elems = for kvp in options(d) seq:
to-string("%~ = %~" % [key(kvp), value(kvp)])
Expand Down Expand Up @@ -116,10 +106,6 @@ public defn parse-pkg-dependency (name:String, table:TomlTable -- env-sub-enable
value(x)
PkgDependency(name, version, type, options)

public defn fetch-or-sync-pkgver (d: PkgDependency, parent-name:String|False = false, dep-path:String|False = false) -> Maybe<PkgDependency>:
fetch-dependency-pkgver(d)
to-maybe(d)

public defn fetch-dependency-pkgver (d: PkgDependency) -> False:
info("fetching %_ at %_" % [colored-name?(d), colored-version?(d)])
info("fetching with options: \"%_\"" % [entries(options(d))])
Expand Down
6 changes: 3 additions & 3 deletions src/task.stanza
Original file line number Diff line number Diff line change
Expand Up @@ -163,9 +163,6 @@ public defstruct TaskDependency <: Dependency & Hashable & Equalable:
with:
constructor => #TaskDependency

defmethod recursive (d:TaskDependency) -> True|False :
false

defmethod path (d:TaskDependency) -> String:
path-join(SLM_DEPS_DIR, name(d))

Expand All @@ -180,6 +177,9 @@ defmethod hash (x:TaskDependency) -> Int :
hash(k) + hash(v)
hash(name(x)) + hash(task?(x)) + ptask-hash

defmethod dependency-type-name (d:TaskDependency) -> String :
"task"

public defn TaskDependency (
name:String,
--
Expand Down
22 changes: 17 additions & 5 deletions src/toml.stanza
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,24 @@ This will replace strings like `{HOME}` with environment variable
values if they exist.
<DOC>
public defn parse-slm-toml-file (file:String -- env-sub-enable:True|False = true) -> SlmToml:
val [dir, _] = split-filepath(file)
val table = file $> parse-file $> table

val name = table["name"] as String
defn get-str-or-err (key:String) -> String :
match(get?(table, key)) :
(x:One<TomlValue>) :
match(value(x)) :
(s:String) : s
(x) :
error("%_ is invalid: value of '%_' key is not a string" % [file,
key])
(_) :
error("%_ is invalid: '%_' key not found" % [file, key])

val name = get-str-or-err("name")
debug("parse-slm-toml: name: \"%_\"" % [name])

val version = table["version"] as String
val version = get-str-or-err("version")
debug("parse-slm-toml: version: \"%_\"" % [version])

val compiler? = get-str?(table, "compiler")
Expand All @@ -94,7 +106,7 @@ public defn parse-slm-toml-file (file:String -- env-sub-enable:True|False = true
parse-git-dependency(name, legacy)
(table:TomlTable):
debug("parse-slm-toml: calling parse-dependency(\"%_\", %_, %_)" % [name, table, env-sub-enable])
parse-dependency(name, table, env-sub-enable)
parse-dependency(name, table, dir, env-sub-enable)

SlmToml(name, version, compiler?, stanza-version?, dependencies)

Expand All @@ -105,7 +117,7 @@ public defstruct InvalidDependencyConfigError <: Exception :
defmethod print (o:OutputStream, e:InvalidDependencyConfigError) :
print(o, "Invalid Dependency[%_]: %_" % [name(e), msg(e)])

defn parse-dependency (name:String, table:TomlTable, env-sub-enable:True|False) -> Dependency :
defn parse-dependency (name:String, table:TomlTable, toml-dir:String, env-sub-enable:True|False) -> Dependency :
val gitAttr = get-str?(table, "git")
val pathAttr = get-str?(table, "path")
val pkgAttr = get-str?(table, "pkg")
Expand All @@ -121,7 +133,7 @@ defn parse-dependency (name:String, table:TomlTable, env-sub-enable:True|False)
match(pathAttr, gitAttr, pkgAttr, taskAttr):
(p:One<String>, g:None, k: None, t:None): ; Path Dependency
debug("parse-dependency: calling parse-path-dependency(\"%_\", ...)" % [name])
parse-path-dependency(name, value(p), version, env-sub-enable)
parse-path-dependency(name, value(p), version, toml-dir, env-sub-enable)
(p:None, g:One<String>, k: None, t:None): ; Git Dependency
debug("parse-dependency: calling parse-git-dependency(\"%_\", ...)" % [name])
parse-git-dependency(name, value(g), version)
Expand Down