Skip to content

Commit

Permalink
fix(website): 🐛 fetched examples from git so they don't go obsolete i…
Browse files Browse the repository at this point in the history
…n older versions

Fixes #60.
  • Loading branch information
arctic-hen7 committed Oct 10, 2021
1 parent ba4caf6 commit 5608a6a
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 10 deletions.
7 changes: 6 additions & 1 deletion docs/manifest.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
{
"stable": "0.2.x",
"outdated": ["0.1.x"],
"beta": ["0.3.x"]
"beta": ["0.3.x"],
"history_map": {
"0.1.x": "v0.1.4",
"0.2.x": "v0.2.3",
"0.3.x": "HEAD"
}
}
48 changes: 39 additions & 9 deletions website/website/src/templates/docs/generation.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
use crate::templates::docs::get_file_at_version::get_file_at_version;
use crate::templates::docs::icons::{ERROR_ICON, WARNING_ICON};
use crate::templates::docs::template::DocsPageProps;
use lazy_static::lazy_static;
use perseus::{path_prefix::get_path_prefix_server, t, RenderFnResult, RenderFnResultWithCause};
use pulldown_cmark::{html, Options, Parser};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::fs;
use std::path::PathBuf;
use sycamore::prelude::Template as SycamoreTemplate;
Expand Down Expand Up @@ -99,6 +101,8 @@ pub struct DocsManifest {
pub stable: String,
pub outdated: Vec<String>,
pub beta: Vec<String>,
/// A map of versions to points in the Git version history.
pub history_map: HashMap<String, String>,
}

pub async fn get_build_state(path: String, locale: String) -> RenderFnResultWithCause<String> {
Expand Down Expand Up @@ -141,6 +145,7 @@ pub async fn get_build_state(path: String, locale: String) -> RenderFnResultWith
let fs_path = format!("../../../{}.md", fs_path);
// Read that file
let contents = fs::read_to_string(&fs_path)?;

// Handle the directives to include code from another file
// We only loop through the file's lines if it likely contains what we want
let contents = if contents.contains("{{#") {
Expand All @@ -158,9 +163,21 @@ pub async fn get_build_state(path: String, locale: String) -> RenderFnResultWith
while let Some(new_path) = incl_path.strip_prefix("../") {
incl_path = new_path;
}
// And now add a `../../../` to the front so that it's relative from `.perseus/`, where we are now
let rel_incl_path = format!("../../../{}", &incl_path);
let incl_contents = fs::read_to_string(&rel_incl_path)?;
// If we're on the `next` version, read from the filesystem directly
// Otherwise, use Git to get the appropriate version (otherwise we get #60)
let incl_contents = if version == "next" {
// Add a `../../../` to the front so that it's relative from `.perseus/`, where we are now
fs::read_to_string(format!("../../../{}", &incl_path))?
} else {
// Get the corresponding history point for this version
let history_point = DOCS_MANIFEST.history_map.get(version);
let history_point = match history_point {
Some(history_point) => history_point,
None => panic!("docs version '{}' not present in history map", version),
};
// We want the path relative to the root of the project directory (where the Git repo is)
get_file_at_version(incl_path, history_point, PathBuf::from("../../../"))?
};
// Now replace the whole directive (trimmed though to preserve any whitespace) with the file's contents
contents_with_incls = contents_with_incls.replace(&line, &incl_contents);
} else if line.starts_with("{{#lines_include ") && line.ends_with("}}") {
Expand All @@ -179,9 +196,22 @@ pub async fn get_build_state(path: String, locale: String) -> RenderFnResultWith
let vec: Vec<&str> = incl_path_with_lines_suffix.split(':').collect();
(vec[0], vec[1].parse::<usize>()?, vec[2].parse::<usize>()?)
};
// And now add a `../../../` to the front so that it's relative from `.perseus/`, where we are now
let rel_incl_path = format!("../../../{}", &incl_path);
let incl_contents_full = fs::read_to_string(&rel_incl_path)?;
// TODO use Git to get file contents if we're no on the `next` version
// If we're on the `next` version, read from the filesystem directly
// Otherwise, use Git to get the appropriate version (otherwise we get #60)
let incl_contents_full = if version == "next" {
// Add a `../../../` to the front so that it's relative from `.perseus/`, where we are now
fs::read_to_string(format!("../../../{}", &incl_path))?
} else {
// Get the corresponding history point for this version
let history_point = DOCS_MANIFEST.history_map.get(version);
let history_point = match history_point {
Some(history_point) => history_point,
None => panic!("docs version '{}' not present in history map", version),
};
// We want the path relative to the root of the project directory (where the Git repo is)
get_file_at_version(incl_path, history_point, PathBuf::from("../../../"))?
};
// Get the specific lines wanted
let incl_contents_lines = incl_contents_full
.lines()
Expand Down Expand Up @@ -233,14 +263,14 @@ pub async fn get_build_state(path: String, locale: String) -> RenderFnResultWith
// Work out the status of this page
let status = if version == "next" {
DocsVersionStatus::Next
} else if DOCS_MANIFEST.outdated.contains(&version.to_string()) {
} else if DOCS_MANIFEST.outdated.iter().any(|v| v == version) {
DocsVersionStatus::Outdated
} else if DOCS_MANIFEST.beta.contains(&version.to_string()) {
} else if DOCS_MANIFEST.beta.iter().any(|v| v == version) {
DocsVersionStatus::Beta
} else if DOCS_MANIFEST.stable == version {
DocsVersionStatus::Stable
} else {
unreachable!()
panic!("version '{}' isn't listed in the docs manifest", version)
};

let props = DocsPageProps {
Expand Down
19 changes: 19 additions & 0 deletions website/website/src/templates/docs/get_file_at_version.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
use std::path::PathBuf;
use std::process::Command;

/// Gets the contents of the given filename at the given Git version by executing `git show`. This given filename MUST be relative to
/// the root directory of `git_dir`.
pub fn get_file_at_version(
filename: &str,
version: &str,
git_dir: PathBuf,
) -> Result<String, std::io::Error> {
let output = Command::new("git")
.args(["show", &format!("{}:{}", version, filename)])
.current_dir(git_dir)
.output()?;
// TODO error handling
let contents = String::from_utf8(output.stdout)
.map_err(|_| std::io::Error::from(std::io::ErrorKind::InvalidData))?;
Ok(contents)
}
1 change: 1 addition & 0 deletions website/website/src/templates/docs/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
mod container;
mod generation;
mod get_file_at_version;
mod icons;
mod template;

Expand Down

0 comments on commit 5608a6a

Please sign in to comment.