Skip to content

Commit

Permalink
Plumbing!
Browse files Browse the repository at this point in the history
  • Loading branch information
lpil committed Dec 2, 2024
1 parent f052938 commit bbee913
Show file tree
Hide file tree
Showing 4 changed files with 153 additions and 227 deletions.
142 changes: 52 additions & 90 deletions compiler-cli/src/docs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::time::{Instant, SystemTime};

use camino::{Utf8Path, Utf8PathBuf};

use crate::{cli, fs::ProjectIO, hex::ApiKeyCommand, http::HttpClient};
use crate::{cli, fs::ProjectIO, http::HttpClient};
use gleam_core::{
analyse::TargetSupport,
build::{Codegen, Compile, Mode, Options, Package, Target},
Expand All @@ -15,42 +15,21 @@ use gleam_core::{
};

pub fn remove(package: String, version: String) -> Result<()> {
RemoveCommand::new(package, version).run()
}

struct RemoveCommand {
package: String,
version: String,
}

impl RemoveCommand {
pub fn new(package: String, version: String) -> Self {
Self { package, version }
}
}

impl ApiKeyCommand for RemoveCommand {
fn with_api_key(
&mut self,
handle: &tokio::runtime::Handle,
hex_config: &hexpm::Config,
api_key: &str,
) -> Result<()> {
let http = HttpClient::new();

// Remove docs from API
let request = hexpm::remove_docs_request(&self.package, &self.version, api_key, hex_config)
.map_err(Error::hex)?;
let response = handle.block_on(http.send(request))?;
hexpm::remove_docs_response(response).map_err(Error::hex)?;

// Done!
println!(
"The docs for {} {} have been removed from HexDocs",
self.package, self.version
);
Ok(())
}
let runtime = tokio::runtime::Runtime::new().expect("Unable to start Tokio async runtime");
let hex_config = hexpm::Config::new();
let api_key =
crate::hex::HexAuthentication::new(&runtime, hex_config.clone()).get_or_create_api_key()?;
let http = HttpClient::new();

// Remove docs from API
let request = hexpm::remove_docs_request(&package, &version, &api_key, &hex_config)
.map_err(Error::hex)?;
let response = runtime.block_on(http.send(request))?;
hexpm::remove_docs_response(response).map_err(Error::hex)?;

// Done!
println!("The docs for {package} {version} have been removed from HexDocs");
Ok(())
}

#[derive(Debug)]
Expand Down Expand Up @@ -146,60 +125,43 @@ pub(crate) fn build_documentation(
Ok(outputs)
}

struct PublishCommand {
config: PackageConfig,
archive: Vec<u8>,
}

pub fn publish() -> Result<()> {
PublishCommand::new()?.run()
}
let paths = crate::find_project_paths()?;
let config = crate::config::root_config()?;

impl PublishCommand {
pub fn new() -> Result<Self> {
let paths = crate::find_project_paths()?;
let config = crate::config::root_config()?;

// Reset the build directory so we know the state of the project
crate::fs::delete_directory(&paths.build_directory_for_target(Mode::Prod, config.target))?;

let mut built = crate::build::main(
Options {
root_target_support: TargetSupport::Enforced,
warnings_as_errors: false,
codegen: Codegen::All,
compile: Compile::All,
mode: Mode::Prod,
target: None,
no_print_progress: false,
},
crate::build::download_dependencies(cli::Reporter::new())?,
)?;
let outputs =
build_documentation(&config, &mut built.root_package, DocContext::HexPublish)?;
let archive = crate::fs::create_tar_archive(outputs)?;
Ok(Self { config, archive })
}
}
let runtime = tokio::runtime::Runtime::new().expect("Unable to start Tokio async runtime");
let hex_config = hexpm::Config::new();
let api_key =
crate::hex::HexAuthentication::new(&runtime, hex_config.clone()).get_or_create_api_key()?;

impl ApiKeyCommand for PublishCommand {
fn with_api_key(
&mut self,
handle: &tokio::runtime::Handle,
hex_config: &hexpm::Config,
api_key: &str,
) -> Result<()> {
let start = Instant::now();
cli::print_publishing_documentation();
handle.block_on(hex::publish_documentation(
&self.config.name,
&self.config.version,
std::mem::take(&mut self.archive),
api_key,
hex_config,
&HttpClient::new(),
))?;
cli::print_published(start.elapsed());
Ok(())
}
// Reset the build directory so we know the state of the project
crate::fs::delete_directory(&paths.build_directory_for_target(Mode::Prod, config.target))?;

let mut built = crate::build::main(
Options {
root_target_support: TargetSupport::Enforced,
warnings_as_errors: false,
codegen: Codegen::All,
compile: Compile::All,
mode: Mode::Prod,
target: None,
no_print_progress: false,
},
crate::build::download_dependencies(cli::Reporter::new())?,
)?;
let outputs = build_documentation(&config, &mut built.root_package, DocContext::HexPublish)?;
let archive = crate::fs::create_tar_archive(outputs)?;

let start = Instant::now();
cli::print_publishing_documentation();
runtime.block_on(hex::publish_documentation(
&config.name,
&config.version,
archive,
&api_key,
&hex_config,
&HttpClient::new(),
))?;
cli::print_published(start.elapsed());
Ok(())
}
18 changes: 6 additions & 12 deletions compiler-cli/src/hex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ use gleam_core::{
Error, Result,
};

pub use auth::HexAuthentication;

pub fn retire(
package: String,
version: String,
Expand All @@ -15,8 +17,7 @@ pub fn retire(
) -> Result<()> {
let runtime = tokio::runtime::Runtime::new().expect("Unable to start Tokio async runtime");
let config = hexpm::Config::new();
let api_key =
auth::HexAuthentication::new(runtime.clone(), config.clone()).get_or_create_api_key()?;
let api_key = HexAuthentication::new(&runtime, config.clone()).get_or_create_api_key()?;

runtime.block_on(hex::retire_release(
&package,
Expand All @@ -34,8 +35,7 @@ pub fn retire(
pub fn unretire(package: String, version: String) -> Result<()> {
let runtime = tokio::runtime::Runtime::new().expect("Unable to start Tokio async runtime");
let config = hexpm::Config::new();
let api_key =
auth::HexAuthentication::new(runtime.clone(), config.clone()).get_or_create_api_key()?;
let api_key = HexAuthentication::new(&runtime, config.clone()).get_or_create_api_key()?;

runtime.block_on(hex::unretire_release(
&package,
Expand All @@ -48,11 +48,6 @@ pub fn unretire(package: String, version: String) -> Result<()> {
Ok(())
}

pub struct RevertCommand {
package: String,
version: String,
}

pub fn revert(package: Option<String>, version: Option<String>) -> Result<()> {
let (package, version) = match (package, version) {
(Some(pkg), Some(ver)) => (pkg, ver),
Expand All @@ -78,8 +73,7 @@ pub fn revert(package: Option<String>, version: Option<String>) -> Result<()> {

let runtime = tokio::runtime::Runtime::new().expect("Unable to start Tokio async runtime");
let hex_config = hexpm::Config::new();
let api_key = auth::HexAuthentication::new(runtime.clone(), hex_config.clone())
.get_or_create_api_key()?;
let api_key = HexAuthentication::new(&runtime, hex_config.clone()).get_or_create_api_key()?;
let http = HttpClient::new();

// Revert release from API
Expand All @@ -96,7 +90,7 @@ pub fn revert(package: Option<String>, version: Option<String>) -> Result<()> {
pub(crate) fn authenticate() -> Result<()> {
let runtime = tokio::runtime::Runtime::new().expect("Unable to start Tokio async runtime");
let config = hexpm::Config::new();
let mut auth = auth::HexAuthentication::new(runtime, config.clone());
let mut auth = HexAuthentication::new(&runtime, config.clone());

if auth.has_stored_api_key() {
let question = "You already have a local Hex API token. Would you
Expand Down
8 changes: 4 additions & 4 deletions compiler-cli/src/hex/auth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,18 @@ pub struct UnencryptedApiKey {
unencrypted: String,
}

pub struct HexAuthentication {
runtime: tokio::runtime::Runtime,
pub struct HexAuthentication<'runtime> {
runtime: &'runtime tokio::runtime::Runtime,
http: HttpClient,
stored_api_key_path: Utf8PathBuf,
local_password: Option<String>,
hex_config: hexpm::Config,
}

impl HexAuthentication {
impl<'runtime> HexAuthentication<'runtime> {
/// Reads the stored API key from disc, if it exists.
///
pub fn new(runtime: tokio::runtime::Runtime, hex_config: hexpm::Config) -> Self {
pub fn new(runtime: &'runtime tokio::runtime::Runtime, hex_config: hexpm::Config) -> Self {
Self {
runtime,
http: HttpClient::new(),
Expand Down
Loading

0 comments on commit bbee913

Please sign in to comment.