-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add `Deployments` preprocessor to `forge doc` Remove reference project :broom: * Add optional relative path / remove short flag
- Loading branch information
Showing
9 changed files
with
153 additions
and
10 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -41,3 +41,5 @@ toml = "0.7" | |
auto_impl = "1" | ||
derive_more = "0.99" | ||
once_cell = "1" | ||
serde = "1.0.163" | ||
serde_json = "1.0.96" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
use ethers_core::types::Address; | ||
|
||
use super::{Preprocessor, PreprocessorId}; | ||
use crate::{Document, PreprocessorOutput}; | ||
use std::{fs, path::PathBuf}; | ||
|
||
/// [Deployments] preprocessor id. | ||
pub const DEPLOYMENTS_ID: PreprocessorId = PreprocessorId("deployments"); | ||
|
||
/// The deployments preprocessor. | ||
/// | ||
/// This preprocessor writes to [Document]'s context. | ||
#[derive(Debug)] | ||
pub struct Deployments { | ||
/// The project root. | ||
pub root: PathBuf, | ||
/// The deployments directory. | ||
pub deployments: Option<PathBuf>, | ||
} | ||
|
||
/// A contract deployment. | ||
#[derive(serde::Deserialize, Debug, Clone)] | ||
pub struct Deployment { | ||
/// The contract address | ||
pub address: Address, | ||
/// The network name | ||
pub network: Option<String>, | ||
} | ||
|
||
impl Preprocessor for Deployments { | ||
fn id(&self) -> PreprocessorId { | ||
DEPLOYMENTS_ID | ||
} | ||
|
||
fn preprocess(&self, documents: Vec<Document>) -> Result<Vec<Document>, eyre::Error> { | ||
let deployments_dir = | ||
self.root.join(self.deployments.as_ref().unwrap_or(&PathBuf::from("deployments"))); | ||
|
||
// Gather all networks from the deployments directory. | ||
let networks = fs::read_dir(&deployments_dir)? | ||
.map(|x| { | ||
x.map(|y| { | ||
if y.file_type()?.is_dir() { | ||
Ok(y.file_name().into_string().map_err(|e| { | ||
eyre::eyre!("Failed to extract directory name: {:?}", e) | ||
})?) | ||
} else { | ||
eyre::bail!("Not a directory.") | ||
} | ||
})? | ||
}) | ||
.collect::<Result<Vec<_>, _>>()?; | ||
|
||
// Iterate over all documents to find any deployments. | ||
for document in documents.iter() { | ||
let mut deployments = Vec::default(); | ||
|
||
// Iterate over all networks and check if there is a deployment for the given contract. | ||
for network in &networks { | ||
// Clone the item path of the document and change it from ".sol" -> ".json" | ||
let mut item_path_clone = document.item_path.clone(); | ||
item_path_clone.set_extension("json"); | ||
|
||
// Determine the path of the deployment artifact relative to the root directory. | ||
let deployment_path = deployments_dir.join(network).join( | ||
item_path_clone | ||
.file_name() | ||
.ok_or(eyre::eyre!("Failed to extract file name from item path"))?, | ||
); | ||
|
||
// If the deployment file for the given contract is found, add the deployment | ||
// address to the document context. | ||
let mut deployment: Deployment = | ||
serde_json::from_str(&fs::read_to_string(deployment_path)?)?; | ||
deployment.network = Some(network.clone()); | ||
deployments.push(deployment); | ||
} | ||
|
||
// If there are any deployments for the given contract, add them to the document | ||
// context. | ||
if !deployments.is_empty() { | ||
document.add_context(self.id(), PreprocessorOutput::Deployments(deployments)); | ||
} | ||
} | ||
|
||
Ok(documents) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters