diff options
author | Eduardo Julian | 2021-07-06 21:34:21 -0400 |
---|---|---|
committer | Eduardo Julian | 2021-07-06 21:34:21 -0400 |
commit | 2b909032e7a0bd10cd7db52067d2fb701bfa95e5 (patch) | |
tree | 0e2aaef228f80f3336715327f7f34065c309de22 /stdlib/source/lux/tool | |
parent | 5cf4efa861075f8276f43a2516f5beacaf610b44 (diff) |
Simplified the API for file-system operations.
Diffstat (limited to 'stdlib/source/lux/tool')
5 files changed, 154 insertions, 192 deletions
diff --git a/stdlib/source/lux/tool/compiler/meta/io/archive.lux b/stdlib/source/lux/tool/compiler/meta/io/archive.lux index 2006fcd79..b6bf39c18 100644 --- a/stdlib/source/lux/tool/compiler/meta/io/archive.lux +++ b/stdlib/source/lux/tool/compiler/meta/io/archive.lux @@ -1,10 +1,11 @@ (.module: [lux (#- Module) - ["@" target (#+ Target)] + [target (#+ Target)] [abstract [predicate (#+ Predicate)] ["." monad (#+ do)]] [control + [pipe (#+ case>)] ["." try (#+ Try)] ["." exception (#+ exception:)] [concurrency @@ -25,7 +26,7 @@ [number ["n" nat]]] [world - ["." file (#+ Path File Directory)]]] + ["." file]]] [program [compositor [import (#+ Import)] @@ -49,7 +50,7 @@ ["." directive] ["#/." program]]]]]]) -(exception: #export (cannot_prepare {archive Path} +(exception: #export (cannot_prepare {archive file.Path} {module_id archive.ID} {error Text}) (exception.report @@ -57,111 +58,101 @@ ["Module ID" (%.nat module_id)] ["Error" error])) -(def: (archive system static) - (All [!] (-> (file.System !) Static Path)) +(def: (archive fs static) + (All [!] (-> (file.System !) Static file.Path)) (format (get@ #static.target static) - (\ system separator) + (\ fs separator) (get@ #static.host static))) -(def: (unversioned_lux_archive system static) - (All [!] (-> (file.System !) Static Path)) - (format (..archive system static) - (\ system separator) +(def: (unversioned_lux_archive fs static) + (All [!] (-> (file.System !) Static file.Path)) + (format (..archive fs static) + (\ fs separator) //.lux_context)) -(def: (versioned_lux_archive system static) - (All [!] (-> (file.System !) Static Path)) - (format (..unversioned_lux_archive system static) - (\ system separator) +(def: (versioned_lux_archive fs static) + (All [!] (-> (file.System !) Static file.Path)) + (format (..unversioned_lux_archive fs static) + (\ fs separator) (%.nat version.version))) -(def: (module system static module_id) - (All [!] (-> (file.System !) Static archive.ID Path)) - (format (..versioned_lux_archive system static) - (\ system separator) +(def: (module fs static module_id) + (All [!] (-> (file.System !) Static archive.ID file.Path)) + (format (..versioned_lux_archive fs static) + (\ fs separator) (%.nat module_id))) -(def: #export (artifact system static module_id artifact_id) - (All [!] (-> (file.System !) Static archive.ID artifact.ID Path)) - (format (..module system static module_id) - (\ system separator) +(def: #export (artifact fs static module_id artifact_id) + (All [!] (-> (file.System !) Static archive.ID artifact.ID file.Path)) + (format (..module fs static module_id) + (\ fs separator) (%.nat artifact_id) (get@ #static.artifact_extension static))) -(def: #export (prepare system static module_id) +(def: (ensure_directory fs path) + (-> (file.System Promise) file.Path (Promise (Try Any))) + (do promise.monad + [? (\ fs directory? path)] + (if ? + (wrap (#try.Success [])) + (\ fs make_directory path)))) + +(def: #export (prepare fs static module_id) (-> (file.System Promise) Static archive.ID (Promise (Try Any))) (do {! promise.monad} - [#let [module (..module system static module_id)] - module_exists? (file.exists? promise.monad system module)] + [#let [module (..module fs static module_id)] + module_exists? (\ fs directory? module)] (if module_exists? (wrap (#try.Success [])) - (do ! - [_ (file.get_directory ! system (..unversioned_lux_archive system static)) - _ (file.get_directory ! system (..versioned_lux_archive system static)) - outcome (\ system create_directory module)] - (case outcome - (#try.Success output) - (wrap (#try.Success [])) - - (#try.Failure error) - (wrap (exception.throw ..cannot_prepare [(..archive system static) - module_id - error]))))))) - -(def: #export (write system static module_id artifact_id content) + (do (try.with !) + [_ (ensure_directory fs (..unversioned_lux_archive fs static)) + _ (ensure_directory fs (..versioned_lux_archive fs static))] + (|> module + (\ fs make_directory) + (\ ! map (|>> (case> (#try.Success output) + (#try.Success []) + + (#try.Failure error) + (exception.throw ..cannot_prepare [(..archive fs static) + module_id + error]))))))))) + +(def: #export (write fs static module_id artifact_id content) (-> (file.System Promise) Static archive.ID artifact.ID Binary (Promise (Try Any))) - (do (try.with promise.monad) - [artifact (: (Promise (Try (File Promise))) - (file.get_file promise.monad system - (..artifact system static module_id artifact_id)))] - (\ artifact over_write content))) + (\ fs write content (..artifact fs static module_id artifact_id))) -(def: #export (enable system static) +(def: #export (enable fs static) (-> (file.System Promise) Static (Promise (Try Any))) (do (try.with promise.monad) - [_ (: (Promise (Try (Directory Promise))) - (file.get_directory promise.monad system (get@ #static.target static))) - _ (: (Promise (Try (Directory Promise))) - (file.get_directory promise.monad system (..archive system static)))] - (wrap []))) - -(def: (general_descriptor system static) - (-> (file.System Promise) Static Path) - (format (..archive system static) - (\ system separator) + [_ (..ensure_directory fs (get@ #static.target static))] + (..ensure_directory fs (..archive fs static)))) + +(def: (general_descriptor fs static) + (-> (file.System Promise) Static file.Path) + (format (..archive fs static) + (\ fs separator) "general_descriptor")) -(def: #export (freeze system static archive) +(def: #export (freeze fs static archive) (-> (file.System Promise) Static Archive (Promise (Try Any))) - (do (try.with promise.monad) - [file (: (Promise (Try (File Promise))) - (file.get_file promise.monad system (..general_descriptor system static)))] - (\ file over_write (archive.export ///.version archive)))) + (\ fs write (archive.export ///.version archive) (..general_descriptor fs static))) (def: module_descriptor_file "module_descriptor") -(def: (module_descriptor system static module_id) - (-> (file.System Promise) Static archive.ID Path) - (format (..module system static module_id) - (\ system separator) +(def: (module_descriptor fs static module_id) + (-> (file.System Promise) Static archive.ID file.Path) + (format (..module fs static module_id) + (\ fs separator) ..module_descriptor_file)) -(def: #export (cache system static module_id content) +(def: #export (cache fs static module_id content) (-> (file.System Promise) Static archive.ID Binary (Promise (Try Any))) - (do (try.with promise.monad) - [file (: (Promise (Try (File Promise))) - (file.get_file promise.monad system - (..module_descriptor system static module_id)))] - (\ file over_write content))) + (\ fs write content (..module_descriptor fs static module_id))) -(def: (read_module_descriptor system static module_id) +(def: (read_module_descriptor fs static module_id) (-> (file.System Promise) Static archive.ID (Promise (Try Binary))) - (do (try.with promise.monad) - [file (: (Promise (Try (File Promise))) - (file.get_file promise.monad system - (..module_descriptor system static module_id)))] - (\ file content []))) + (\ fs read (..module_descriptor fs static module_id))) (def: parser (Parser [Descriptor (Document .Module)]) @@ -184,24 +175,20 @@ (archive.archived archive)))] (wrap (set@ #.modules modules (fresh_analysis_state host))))) -(def: (cached_artifacts system static module_id) +(def: (cached_artifacts fs static module_id) (-> (file.System Promise) Static archive.ID (Promise (Try (Dictionary Text Binary)))) - (do {! (try.with promise.monad)} - [module_dir (\ system directory (..module system static module_id)) - cached_files (\ module_dir files [])] - (|> cached_files - (list\map (function (_ file) - [(file.name system (\ file path)) - (\ file path)])) - (list.filter (|>> product.left (text\= ..module_descriptor_file) not)) - (monad.map ! (function (_ [name path]) - (do ! - [file (: (Promise (Try (File Promise))) - (\ system file path)) - data (: (Promise (Try Binary)) - (\ file content []))] - (wrap [name data])))) - (\ ! map (dictionary.from_list text.hash))))) + (let [! (try.with promise.monad)] + (|> (..module fs static module_id) + (\ fs directory_files) + (\ ! map (|>> (list\map (function (_ file) + [(file.name fs file) file])) + (list.filter (|>> product.left (text\= ..module_descriptor_file) not)) + (monad.map ! (function (_ [name path]) + (|> path + (\ fs read) + (\ ! map (|>> [name]))))) + (\ ! map (dictionary.from_list text.hash)))) + (\ ! join)))) (type: Definitions (Dictionary Text Any)) (type: Analysers (Dictionary Text analysis.Handler)) @@ -321,27 +308,27 @@ (wrap [(document.write $.key (set@ #.definitions definitions content)) bundles]))) -(def: (load_definitions system static module_id host_environment [descriptor document output]) +(def: (load_definitions fs static module_id host_environment [descriptor document output]) (All [expression directive] (-> (file.System Promise) Static archive.ID (generation.Host expression directive) [Descriptor (Document .Module) Output] (Promise (Try [[Descriptor (Document .Module) Output] Bundles])))) (do (try.with promise.monad) - [actual (cached_artifacts system static module_id) + [actual (cached_artifacts fs static module_id) #let [expected (|> descriptor (get@ #descriptor.registry) artifact.artifacts)] [document bundles] (promise\wrap (loaded_document (get@ #static.artifact_extension static) host_environment module_id expected actual document))] (wrap [[descriptor document output] bundles]))) -(def: (purge! system static [module_name module_id]) +(def: (purge! fs static [module_name module_id]) (-> (file.System Promise) Static [Module archive.ID] (Promise (Try Any))) (do {! (try.with promise.monad)} - [cache (\ system directory (..module system static module_id)) - artifacts (\ cache files []) - _ (monad.map ! (function (_ artifact) - (\ artifact delete [])) - artifacts)] - (\ cache discard []))) + [#let [cache (..module fs static module_id)] + _ (|> cache + (\ fs directory_files) + (\ ! map (monad.map ! (\ fs delete))) + (\ ! join))] + (\ fs delete cache))) (def: (valid_cache? expected actual) (-> Descriptor Input Bit) @@ -386,7 +373,7 @@ Text "(Lux Caching System)") -(def: (load_every_reserved_module host_environment system static import contexts archive) +(def: (load_every_reserved_module host_environment fs static import contexts archive) (All [expression directive] (-> (generation.Host expression directive) (file.System Promise) Static Import (List Context) Archive (Promise (Try [Archive .Lux Bundles])))) @@ -395,13 +382,13 @@ archive.reservations (monad.map ! (function (_ [module_name module_id]) (do ! - [data (..read_module_descriptor system static module_id) + [data (..read_module_descriptor fs static module_id) [descriptor document] (promise\wrap (<binary>.run ..parser data))] (if (text\= archive.runtime_module module_name) (wrap [true [module_name [module_id [descriptor document (: Output row.empty)]]]]) (do ! - [input (//context.read system ..pseudo_module import contexts (get@ #static.host_module_extension static) module_name)] + [input (//context.read fs ..pseudo_module import contexts (get@ #static.host_module_extension static) module_name)] (wrap [(..valid_cache? descriptor input) [module_name [module_id [descriptor document (: Output row.empty)]]]]))))))) load_order (|> pre_loaded_caches @@ -416,13 +403,13 @@ #let [purge (..full_purge pre_loaded_caches load_order)] _ (|> purge dictionary.entries - (monad.map ! (..purge! system static))) + (monad.map ! (..purge! fs static))) loaded_caches (|> load_order (list.filter (function (_ [module_name [module_id [descriptor document output]]]) (not (dictionary.key? purge module_name)))) (monad.map ! (function (_ [module_name [module_id descriptor,document,output]]) (do ! - [[descriptor,document,output bundles] (..load_definitions system static module_id host_environment descriptor,document,output)] + [[descriptor,document,output bundles] (..load_definitions fs static module_id host_environment descriptor,document,output)] (wrap [[module_name descriptor,document,output] bundles])))))] (promise\wrap @@ -444,18 +431,17 @@ ..empty_bundles loaded_caches)]))))) -(def: #export (thaw host_environment system static import contexts) +(def: #export (thaw host_environment fs static import contexts) (All [expression directive] (-> (generation.Host expression directive) (file.System Promise) Static Import (List Context) (Promise (Try [Archive .Lux Bundles])))) (do promise.monad - [file (\ system file (..general_descriptor system static))] - (case file - (#try.Success file) + [binary (\ fs read (..general_descriptor fs static))] + (case binary + (#try.Success binary) (do (try.with promise.monad) - [binary (\ file content []) - archive (promise\wrap (archive.import ///.version binary))] - (..load_every_reserved_module host_environment system static import contexts archive)) + [archive (promise\wrap (archive.import ///.version binary))] + (..load_every_reserved_module host_environment fs static import contexts archive)) (#try.Failure error) (wrap (#try.Success [archive.empty diff --git a/stdlib/source/lux/tool/compiler/meta/io/context.lux b/stdlib/source/lux/tool/compiler/meta/io/context.lux index 788be9fed..f31b4e1b2 100644 --- a/stdlib/source/lux/tool/compiler/meta/io/context.lux +++ b/stdlib/source/lux/tool/compiler/meta/io/context.lux @@ -18,7 +18,7 @@ [collection ["." dictionary (#+ Dictionary)]]] [world - ["." file (#+ Path File)]]] + ["." file]]] [program [compositor [import (#+ Import)]]] @@ -44,55 +44,53 @@ Extension ".lux") -(def: #export (path system context module) - (All [m] (-> (file.System m) Context Module Path)) +(def: #export (path fs context module) + (All [m] (-> (file.System m) Context Module file.Path)) (|> module - (//.sanitize system) - (format context (\ system separator)))) + (//.sanitize fs) + (format context (\ fs separator)))) -(def: (find_source_file system importer contexts module extension) +(def: (find_source_file fs importer contexts module extension) (-> (file.System Promise) Module (List Context) Module Extension - (Promise (Try [Path (File Promise)]))) + (Promise (Try file.Path))) (case contexts #.Nil (promise\wrap (exception.throw ..cannot_find_module [importer module])) (#.Cons context contexts') - (do promise.monad - [#let [path (format (..path system context module) extension)] - file (\ system file [path])] - (case file - (#try.Success file) - (wrap (#try.Success [path file])) - - (#try.Failure _) - (find_source_file system importer contexts' module extension))))) + (let [path (format (..path fs context module) extension)] + (do promise.monad + [? (\ fs file? path)] + (if ? + (wrap (#try.Success path)) + (find_source_file fs importer contexts' module extension)))))) (def: (full_host_extension partial_host_extension) (-> Extension Extension) (format partial_host_extension ..lux_extension)) -(def: (find_local_source_file system importer import contexts partial_host_extension module) +(def: (find_local_source_file fs importer import contexts partial_host_extension module) (-> (file.System Promise) Module Import (List Context) Extension Module - (Promise (Try [Path Binary]))) + (Promise (Try [file.Path Binary]))) ## Preference is explicitly being given to Lux files that have a host extension. ## Normal Lux files (i.e. without a host extension) are then picked as fallback files. (do {! promise.monad} - [outcome (..find_source_file system importer contexts module (..full_host_extension partial_host_extension))] + [outcome (..find_source_file fs importer contexts module (..full_host_extension partial_host_extension))] (case outcome - (#try.Success [path file]) - (do (try.with !) - [data (\ file content [])] - (wrap [path data])) + (#try.Success path) + (|> path + (\ fs read) + (\ (try.with !) map (|>> [path]))) (#try.Failure _) - (do (try.with !) - [[path file] (..find_source_file system importer contexts module ..lux_extension) - data (\ file content [])] - (wrap [path data]))))) + (do {! (try.with !)} + [path (..find_source_file fs importer contexts module ..lux_extension)] + (|> path + (\ fs read) + (\ ! map (|>> [path]))))))) (def: (find_library_source_file importer import partial_host_extension module) - (-> Module Import Extension Module (Try [Path Binary])) + (-> Module Import Extension Module (Try [file.Path Binary])) (let [path (format module (..full_host_extension partial_host_extension))] (case (dictionary.get path import) (#.Some data) @@ -107,13 +105,13 @@ #.None (exception.throw ..cannot_find_module [importer module])))))) -(def: (find_any_source_file system importer import contexts partial_host_extension module) +(def: (find_any_source_file fs importer import contexts partial_host_extension module) (-> (file.System Promise) Module Import (List Context) Extension Module - (Promise (Try [Path Binary]))) + (Promise (Try [file.Path Binary]))) ## Preference is explicitly being given to Lux files that have a host extension. ## Normal Lux files (i.e. without a host extension) are then picked as fallback files. (do {! promise.monad} - [outcome (find_local_source_file system importer import contexts partial_host_extension module)] + [outcome (find_local_source_file fs importer import contexts partial_host_extension module)] (case outcome (#try.Success [path data]) (wrap outcome) @@ -121,11 +119,11 @@ (#try.Failure _) (wrap (..find_library_source_file importer import partial_host_extension module))))) -(def: #export (read system importer import contexts partial_host_extension module) +(def: #export (read fs importer import contexts partial_host_extension module) (-> (file.System Promise) Module Import (List Context) Extension Module (Promise (Try Input))) (do (try.with promise.monad) - [[path binary] (..find_any_source_file system importer import contexts partial_host_extension module)] + [[path binary] (..find_any_source_file fs importer import contexts partial_host_extension module)] (case (\ utf8.codec decode binary) (#try.Success code) (wrap {#////.module module @@ -137,53 +135,35 @@ (promise\wrap (exception.throw ..cannot_read_module [module]))))) (type: #export Enumeration - (Dictionary Path Binary)) - -(exception: #export (cannot_clean_path {prefix Path} {path Path}) - (exception.report - ["Prefix" (%.text prefix)] - ["Path" (%.text path)])) - -(def: (clean_path system context path) - (All [!] (-> (file.System !) Context Path (Try Path))) - (let [prefix (format context (\ system separator))] - (case (text.split_with prefix path) - #.None - (exception.throw ..cannot_clean_path [prefix path]) - - (#.Some [_ path]) - (#try.Success path)))) + (Dictionary file.Path Binary)) -(def: (enumerate_context system context enumeration) +(def: (enumerate_context fs directory enumeration) (-> (file.System Promise) Context Enumeration (Promise (Try Enumeration))) (do {! (try.with promise.monad)} - [directory (\ system directory context)] - (loop [directory directory - enumeration enumeration] - (do ! - [files (\ directory files []) - enumeration (monad.fold ! (function (_ file enumeration) - (let [path (\ file path)] - (if (text.ends_with? ..lux_extension path) - (do ! - [path (promise\wrap (..clean_path system context path)) - source_code (\ file content [])] - (promise\wrap - (dictionary.try_put path source_code enumeration))) - (wrap enumeration)))) - enumeration - files) - directories (\ directory directories [])] - (monad.fold ! recur enumeration directories))))) + [enumeration (|> directory + (\ fs directory_files) + (\ ! map (monad.fold ! (function (_ file enumeration) + (if (text.ends_with? ..lux_extension file) + (do ! + [source_code (\ fs read file)] + (promise\wrap + (dictionary.try_put (file.name fs file) source_code enumeration))) + (wrap enumeration))) + enumeration)) + (\ ! join))] + (|> directory + (\ fs sub_directories) + (\ ! map (monad.fold ! (enumerate_context fs) enumeration)) + (\ ! join)))) (def: Action (type (All [a] (Promise (Try a))))) -(def: #export (enumerate system contexts) +(def: #export (enumerate fs contexts) (-> (file.System Promise) (List Context) (Action Enumeration)) (monad.fold (: (Monad Action) (try.with promise.monad)) - (enumerate_context system) + (..enumerate_context fs) (: Enumeration (dictionary.new text.hash)) contexts)) diff --git a/stdlib/source/lux/tool/compiler/meta/packager/jvm.lux b/stdlib/source/lux/tool/compiler/meta/packager/jvm.lux index 86cec2ba1..a89bdc836 100644 --- a/stdlib/source/lux/tool/compiler/meta/packager/jvm.lux +++ b/stdlib/source/lux/tool/compiler/meta/packager/jvm.lux @@ -21,9 +21,7 @@ [target [jvm [encoding - ["." name]]]] - [world - ["." file (#+ File Directory)]]] + ["." name]]]]] [program [compositor ["." static (#+ Static)]]] diff --git a/stdlib/source/lux/tool/compiler/meta/packager/scheme.lux b/stdlib/source/lux/tool/compiler/meta/packager/scheme.lux index 153aa79b5..ac35684ed 100644 --- a/stdlib/source/lux/tool/compiler/meta/packager/scheme.lux +++ b/stdlib/source/lux/tool/compiler/meta/packager/scheme.lux @@ -24,7 +24,7 @@ [time ["." instant (#+ Instant)]] [world - ["." file (#+ Path File Directory)]]] + ["." file]]] [program [compositor ["." static (#+ Static)]]] @@ -72,7 +72,7 @@ (: _.Expression (_.manual ""))))) (def: module_file - (-> archive.ID Path) + (-> archive.ID file.Path) (|>> %.nat (text.suffix ".scm"))) (def: mode diff --git a/stdlib/source/lux/tool/compiler/meta/packager/script.lux b/stdlib/source/lux/tool/compiler/meta/packager/script.lux index 5ddeac0d5..98a011a4c 100644 --- a/stdlib/source/lux/tool/compiler/meta/packager/script.lux +++ b/stdlib/source/lux/tool/compiler/meta/packager/script.lux @@ -14,9 +14,7 @@ ["." utf8]]] [collection ["." row] - ["." list ("#\." functor)]]] - [world - ["." file (#+ File Directory)]]] + ["." list ("#\." functor)]]]] [program [compositor ["." static (#+ Static)]]] |