-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathbuild.rs
157 lines (142 loc) · 5.11 KB
/
build.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
// Copyright 2018-2022 Cargill Incorporated
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
use std::env;
use std::error::Error;
use std::fmt;
use std::fs;
use std::io;
use std::process::Command;
const FORCE_PANDOC: &str = "SPLINTER_FORCE_PANDOC";
const PATH: &str = "PATH";
/// This build script will take the markdown files in the /man directory and convert them to
/// man pages stored in packaging/man. This build script will check if pandoc is installed locally
/// and skip generating the manpages if it is not. If the build should fail if man pages cannot be
/// generated set environment variable SPLINTER_FORCE_PANDOC=true
fn main() -> Result<(), BuildError> {
let paths = env::var(PATH)
.map_err(|_| BuildError("Unable to read PATH environment variable".into()))?;
let mut pandoc_exist = false;
for path in paths.split(':') {
let entries = match fs::read_dir(path) {
Ok(entries) => entries,
Err(err) => {
// skip a directory in the path that cannot be read.
println!("Unable to read path entry {}: {}", path, err);
continue;
}
};
for entry in entries {
let entry = match entry {
Ok(entry) => entry,
Err(err) => {
// skip an entry in the path that cannot be read.
println!("Unable to read entry in {}: {}", path, err);
continue;
}
};
let path = entry.path();
if path.ends_with("pandoc") {
pandoc_exist = true;
break;
}
}
}
if !pandoc_exist {
if let Ok(var) = env::var(FORCE_PANDOC) {
let map_to_build_err = move |_| {
BuildError("Unable to read SPLINTER_FORCE_PANDOC environment variable".into())
};
if var.parse().map_err(map_to_build_err)? {
return Err(BuildError(
"Cannot generate man pages, pandoc is not installed".into(),
));
}
} else {
println!("Skip generating man pages");
return Ok(());
}
}
let entries = match fs::read_dir("man/") {
Ok(entries) => {
match entries
.map(|res| res.map(|e| e.path()))
.collect::<Result<Vec<_>, io::Error>>()
{
Ok(entries) => entries,
Err(err) => {
return Err(BuildError(format!(
"Unable to retrieve entries for man pages: {}",
err
)))
}
}
}
Err(err) => {
return Err(BuildError(format!(
"Unable to read man pages directory: {}",
err
)))
}
};
println!("Markdown files found {:?}", entries);
for entry in entries {
// This conversion would only fail if the filename is not valid UTF-8
let markdown = &entry
.to_str()
.ok_or_else(|| BuildError("Cannot get markdown file path".into()))?
.to_string();
let file = entry
.file_stem()
.ok_or_else(|| BuildError("Cannot get markdown file name".into()))?;
let manpage = &format!(
"packaging/man/{}",
file.to_str()
.ok_or_else(|| BuildError("Cannot get markdown file name".into()))?
);
if markdown.ends_with(".md") {
match Command::new("pandoc")
.args(&["--standalone", "--to", "man", markdown, "-o", manpage])
.status()
{
Ok(status) => {
if status.success() {
println!("Generated man page: {}", manpage);
} else {
println!("Unable to generate man page {} status {}", manpage, status);
}
}
Err(err) => {
return Err(BuildError(format!(
"Unable to generate man page: {} {}",
manpage, err
)))
}
}
}
}
Ok(())
}
pub struct BuildError(String);
impl Error for BuildError {}
// This is the output that will be used for print errors returned from main
impl fmt::Debug for BuildError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.write_str(&self.0)
}
}
impl fmt::Display for BuildError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.write_str(&self.0)
}
}