summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDaniel Silverstone <dsilvers@digital-scurf.org>2021-01-09 16:15:35 +0000
committerDaniel Silverstone <dsilvers@digital-scurf.org>2021-01-10 09:49:25 +0000
commitfcdbd77cc2b909b8a1d0fc8d2e6343bbc278470c (patch)
tree05420ead169a05ae3a2b0a7c4564e230596f1fb0
parente9b941e5218e5e4293bb915b13f831baba246a89 (diff)
downloadsubplot-fcdbd77cc2b909b8a1d0fc8d2e6343bbc278470c.tar.gz
resource: Switch from 'templates' to 'share'
In a general sense, we will want to have more than just template files as resources. This shifts from the concept that the only thing resource-wise that subplot has is templates, to a more general shared resources concept without a default path beyond CWD. Signed-off-by: Daniel Silverstone <dsilvers@digital-scurf.org>
-rw-r--r--.gitignore2
-rwxr-xr-xcheck8
-rw-r--r--share/templates/bash/assert.sh (renamed from templates/bash/assert.sh)0
-rw-r--r--share/templates/bash/cap.sh (renamed from templates/bash/cap.sh)0
-rw-r--r--share/templates/bash/ctx.sh (renamed from templates/bash/ctx.sh)0
-rw-r--r--share/templates/bash/dict.sh (renamed from templates/bash/dict.sh)0
-rw-r--r--share/templates/bash/files.sh (renamed from templates/bash/files.sh)0
-rw-r--r--share/templates/bash/template.sh.tera (renamed from templates/bash/template.sh.tera)0
-rw-r--r--share/templates/bash/template.yaml (renamed from templates/bash/template.yaml)0
-rw-r--r--share/templates/python/asserts.py (renamed from templates/python/asserts.py)0
-rw-r--r--share/templates/python/context.py (renamed from templates/python/context.py)0
-rw-r--r--share/templates/python/context_tests.py (renamed from templates/python/context_tests.py)0
-rw-r--r--share/templates/python/encoding.py (renamed from templates/python/encoding.py)0
-rw-r--r--share/templates/python/encoding_tests.py (renamed from templates/python/encoding_tests.py)0
-rw-r--r--share/templates/python/files.py (renamed from templates/python/files.py)0
-rw-r--r--share/templates/python/main.py (renamed from templates/python/main.py)0
-rw-r--r--share/templates/python/scenarios.py (renamed from templates/python/scenarios.py)0
-rw-r--r--share/templates/python/template.py.tera (renamed from templates/python/template.py.tera)0
-rw-r--r--share/templates/python/template.yaml (renamed from templates/python/template.yaml)0
-rw-r--r--share/templates/rust/template.rs.tera (renamed from templates/rust/template.rs.tera)0
-rw-r--r--share/templates/rust/template.yaml (renamed from templates/rust/template.yaml)0
-rw-r--r--src/bin/sp-codegen.rs17
-rw-r--r--src/codegen.rs9
-rw-r--r--src/resource.rs24
-rw-r--r--subplot.py2
25 files changed, 42 insertions, 20 deletions
diff --git a/.gitignore b/.gitignore
index 38c384f..315f9d5 100644
--- a/.gitignore
+++ b/.gitignore
@@ -5,5 +5,5 @@
test.py
.cargo-checksum.json
debian/cargo_home
-templates/python/__pycache__
+share/templates/python/__pycache__
test.log
diff --git a/check b/check
index efb7195..fb5f9c6 100755
--- a/check
+++ b/check
@@ -28,7 +28,7 @@ TOPDIR=$(pwd)
_codegen() {
$hideok cargo run $quiet --package subplot --bin sp-codegen -- \
- "$1" --output "$2" --templates "${TOPDIR}/templates"
+ "$1" --output "$2" --resources "${TOPDIR}/share"
}
codegen() {
@@ -48,7 +48,7 @@ docgen() {
# Run unit tests for the Python template.
(set -eu
- cd templates/python
+ cd share/templates/python
for x in *_tests.py
do
$hideok echo "Unit tests: $x"
@@ -58,13 +58,13 @@ docgen() {
if command -v flake8 > /dev/null
then
- $hideok flake8 templates/python lib/*.py --exclude=template.py
+ $hideok flake8 share/templates/python lib/*.py
fi
if command -v shellcheck > /dev/null
then
shellcheck check ./*.sh
- find templates/bash -name '*.sh' ! -name template.sh -exec shellcheck '{}' +
+ find share/templates/bash -name '*.sh' -exec shellcheck '{}' +
fi
$hideok cargo build --all-targets
diff --git a/templates/bash/assert.sh b/share/templates/bash/assert.sh
index 43bb11b..43bb11b 100644
--- a/templates/bash/assert.sh
+++ b/share/templates/bash/assert.sh
diff --git a/templates/bash/cap.sh b/share/templates/bash/cap.sh
index 8ea35d8..8ea35d8 100644
--- a/templates/bash/cap.sh
+++ b/share/templates/bash/cap.sh
diff --git a/templates/bash/ctx.sh b/share/templates/bash/ctx.sh
index c9401c6..c9401c6 100644
--- a/templates/bash/ctx.sh
+++ b/share/templates/bash/ctx.sh
diff --git a/templates/bash/dict.sh b/share/templates/bash/dict.sh
index aea5b96..aea5b96 100644
--- a/templates/bash/dict.sh
+++ b/share/templates/bash/dict.sh
diff --git a/templates/bash/files.sh b/share/templates/bash/files.sh
index 50c935d..50c935d 100644
--- a/templates/bash/files.sh
+++ b/share/templates/bash/files.sh
diff --git a/templates/bash/template.sh.tera b/share/templates/bash/template.sh.tera
index 5e92371..5e92371 100644
--- a/templates/bash/template.sh.tera
+++ b/share/templates/bash/template.sh.tera
diff --git a/templates/bash/template.yaml b/share/templates/bash/template.yaml
index 01269dd..01269dd 100644
--- a/templates/bash/template.yaml
+++ b/share/templates/bash/template.yaml
diff --git a/templates/python/asserts.py b/share/templates/python/asserts.py
index c898454..c898454 100644
--- a/templates/python/asserts.py
+++ b/share/templates/python/asserts.py
diff --git a/templates/python/context.py b/share/templates/python/context.py
index d61316e..d61316e 100644
--- a/templates/python/context.py
+++ b/share/templates/python/context.py
diff --git a/templates/python/context_tests.py b/share/templates/python/context_tests.py
index c91350e..c91350e 100644
--- a/templates/python/context_tests.py
+++ b/share/templates/python/context_tests.py
diff --git a/templates/python/encoding.py b/share/templates/python/encoding.py
index 1efb95e..1efb95e 100644
--- a/templates/python/encoding.py
+++ b/share/templates/python/encoding.py
diff --git a/templates/python/encoding_tests.py b/share/templates/python/encoding_tests.py
index 4167aa4..4167aa4 100644
--- a/templates/python/encoding_tests.py
+++ b/share/templates/python/encoding_tests.py
diff --git a/templates/python/files.py b/share/templates/python/files.py
index 6346172..6346172 100644
--- a/templates/python/files.py
+++ b/share/templates/python/files.py
diff --git a/templates/python/main.py b/share/templates/python/main.py
index 87e2782..87e2782 100644
--- a/templates/python/main.py
+++ b/share/templates/python/main.py
diff --git a/templates/python/scenarios.py b/share/templates/python/scenarios.py
index e2703df..e2703df 100644
--- a/templates/python/scenarios.py
+++ b/share/templates/python/scenarios.py
diff --git a/templates/python/template.py.tera b/share/templates/python/template.py.tera
index aa97cf0..aa97cf0 100644
--- a/templates/python/template.py.tera
+++ b/share/templates/python/template.py.tera
diff --git a/templates/python/template.yaml b/share/templates/python/template.yaml
index 73f2510..73f2510 100644
--- a/templates/python/template.yaml
+++ b/share/templates/python/template.yaml
diff --git a/templates/rust/template.rs.tera b/share/templates/rust/template.rs.tera
index c972d37..c972d37 100644
--- a/templates/rust/template.rs.tera
+++ b/share/templates/rust/template.rs.tera
diff --git a/templates/rust/template.yaml b/share/templates/rust/template.yaml
index 110f5df..110f5df 100644
--- a/templates/rust/template.yaml
+++ b/share/templates/rust/template.yaml
diff --git a/src/bin/sp-codegen.rs b/src/bin/sp-codegen.rs
index ca4f1a3..f83cf59 100644
--- a/src/bin/sp-codegen.rs
+++ b/src/bin/sp-codegen.rs
@@ -4,10 +4,13 @@ use std::process::Command;
use anyhow::Result;
use structopt::StructOpt;
-use subplot::{generate_test_program, template_spec, Document, Style, SubplotError, TemplateSpec};
+use subplot::{
+ generate_test_program, resource, template_spec, Document, Style, SubplotError, TemplateSpec,
+};
fn main() -> Result<()> {
let opt = Opt::from_args();
+ opt.resources.handle();
let basedir = subplot::get_basedir_from(&opt.filename)?;
let style = Style::default();
let mut doc = Document::from_file(&basedir, &opt.filename, style)?;
@@ -17,7 +20,7 @@ fn main() -> Result<()> {
std::process::exit(1);
}
- let spec = template_spec(&opt.templates, &doc)?;
+ let spec = template_spec(&doc)?;
generate_test_program(&mut doc, &spec, &opt.output)?;
if opt.run && !run(&spec, &opt.output)? {
@@ -49,14 +52,8 @@ struct Opt {
#[structopt(long, short, help = "Runs generated test program")]
run: bool,
- #[structopt(
- long,
- short,
- help = "Look for code templates in DIR",
- default_value = "/usr/share/subplot",
- name = "DIR"
- )]
- templates: PathBuf,
+ #[structopt(flatten)]
+ resources: resource::ResourceOpts,
}
fn run(spec: &TemplateSpec, filename: &Path) -> subplot::Result<bool> {
diff --git a/src/codegen.rs b/src/codegen.rs
index 063f8a2..93d1c75 100644
--- a/src/codegen.rs
+++ b/src/codegen.rs
@@ -9,19 +9,20 @@ use base64::encode;
use serde::Serialize;
use tera::{Context, Tera, Value};
-use anyhow::Result;
+use anyhow::{Context as AnyhowContext, Result};
/// Return the requested template specification.
-pub fn template_spec(templates: &Path, doc: &Document) -> Result<TemplateSpec> {
+pub fn template_spec(doc: &Document) -> Result<TemplateSpec> {
let template = doc
.meta()
.template_name()
.ok_or(SubplotError::MissingTemplate)?;
- let mut filename = templates.to_path_buf();
+ let mut filename = PathBuf::from("templates");
filename.push(Path::new(template));
filename.push(Path::new("template.yaml"));
- Ok(TemplateSpec::from_file(&filename)?)
+ Ok(TemplateSpec::from_file(&filename)
+ .with_context(|| format!("Failed to read template file: {}", filename.display()))?)
}
/// Generate a test program from a document, using a template spec.
diff --git a/src/resource.rs b/src/resource.rs
index dd30678..48c8cc5 100644
--- a/src/resource.rs
+++ b/src/resource.rs
@@ -8,6 +8,30 @@
use std::io::{self, Read};
use std::path::{Path, PathBuf};
use std::sync::Mutex;
+use structopt::StructOpt;
+
+#[derive(Debug, StructOpt)]
+/// Options which relate to resource management
+///
+/// To use this, include them *flat* in your options struct, and then after
+/// parsing, call the [ResourceOpts::handle()] function.
+pub struct ResourceOpts {
+ #[structopt(
+ long,
+ help = "Look for code templates and other resources in DIR",
+ name = "DIR"
+ )]
+ resources: Option<PathBuf>,
+}
+
+impl ResourceOpts {
+ /// Handle any supplied resource related arguments
+ pub fn handle(&self) {
+ if let Some(rpath) = self.resources.as_ref() {
+ add_search_path(rpath);
+ }
+ }
+}
use lazy_static::lazy_static;
diff --git a/subplot.py b/subplot.py
index 4b0ff13..dbfb309 100644
--- a/subplot.py
+++ b/subplot.py
@@ -12,7 +12,7 @@ import tempfile
wrapper = """\
#!/bin/sh
set -eu
-exec '{srcdir}/target/debug/sp-codegen' "$@" -t '{srcdir}/templates'
+exec '{srcdir}/target/debug/sp-codegen' "$@" --resources '{srcdir}/share'
"""