Skip to content

Changed formatting to comply with rust standards #9

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 14, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions impl/rust/src/commands/fetch_command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use zip::read::ZipArchive;
use super::command::Command;

pub struct FetchCommand<'a> {
pub slice_root_directory: &'a Path
pub slice_root_directory: &'a Path,
}

impl<'a> FetchCommand<'a> {
Expand Down Expand Up @@ -46,7 +46,8 @@ impl<'a> FetchCommand<'a> {
}

fn determine_latest_version() -> Version {
let body = FetchCommand::execute_request_to_uri("https://api.github.com/repos/slicebuild/slices/branches");
let body = FetchCommand::execute_request_to_uri("https://api.github.\
com/repos/slicebuild/slices/branches");
let body = String::from_utf8(body).unwrap();
let json = Json::from_str(&body).unwrap();
let array = json.as_array().unwrap();
Expand All @@ -55,11 +56,13 @@ impl<'a> FetchCommand<'a> {

fn download_latest_version() -> Vec<u8> {
let version = FetchCommand::determine_latest_version();
let uri = format!("https://codeload.github.com/slicebuild/slices/zip/{}", version);
let uri = format!("https://codeload.github.com/slicebuild/slices/zip/{}",
version);
FetchCommand::execute_request_to_uri(&uri)
}

fn extract_archive_into_directory(mut zip_archive: ZipArchive<Cursor<Vec<u8>>>, path: PathBuf) {
fn extract_archive_into_directory(mut zip_archive: ZipArchive<Cursor<Vec<u8>>>,
path: PathBuf) {
for i in 0..zip_archive.len() {
let mut path = path.clone();
let mut file = zip_archive.by_index(i).unwrap();
Expand Down Expand Up @@ -88,6 +91,7 @@ impl<'a> Command for FetchCommand<'a> {
let bytes = FetchCommand::download_latest_version();
let cursor = Cursor::new(bytes);
let zip_archive = ZipArchive::new(cursor).unwrap();
FetchCommand::extract_archive_into_directory(zip_archive, self.slice_root_directory.to_path_buf());
FetchCommand::extract_archive_into_directory(zip_archive,
self.slice_root_directory.to_path_buf());
}
}
16 changes: 10 additions & 6 deletions impl/rust/src/commands/find_command.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
use std::path::Path;
use super::helper::check_slice_root_exists;
use super::command::Command;
use super::super::slice::directory::{get_latest_slices_from_slice_root_directory};
use super::super::slice::directory::get_latest_slices_from_slice_root_directory;
#[cfg(test)]
use super::super::for_testing::get_slice_root_directory;

pub struct FindCommand<'a> {
pub layer: String,
pub os: String,
pub slice_root_directory: &'a Path
pub slice_root_directory: &'a Path,
}

impl<'a> Command for FindCommand<'a> {
Expand All @@ -23,15 +23,19 @@ impl<'a> Command for FindCommand<'a> {
for slice in slices {
println!("{}", slice.name);
}
},
Err(error) => println!("{}", error)
}
Err(error) => println!("{}", error),
}
}
}

#[test]
fn test_find_command_run() {
let slice_root_directory = get_slice_root_directory();
let mut command = FindCommand { layer: "jekyll".to_string(), os: "debian".to_string(), slice_root_directory: &slice_root_directory };
let mut command = FindCommand {
layer: "jekyll".to_string(),
os: "debian".to_string(),
slice_root_directory: &slice_root_directory,
};
command.run();
}
}
17 changes: 9 additions & 8 deletions impl/rust/src/commands/helper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@ use std::fs::metadata;
use std::path::Path;

pub fn check_slice_root_exists(slice_root_directory: &Path) {
if let Err(error) = metadata(slice_root_directory) {
if error.kind() == ErrorKind::NotFound {
println!("Slice root directory is not exists. Path = {}", slice_root_directory.to_str().unwrap());
} else {
println!("Unknown error = {}", error);
}
panic!();
}
if let Err(error) = metadata(slice_root_directory) {
if error.kind() == ErrorKind::NotFound {
println!("Slice root directory is not exists. Path = {}",
slice_root_directory.to_str().unwrap());
} else {
println!("Unknown error = {}", error);
}
panic!();
}
}
133 changes: 75 additions & 58 deletions impl/rust/src/commands/make_command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,83 +7,100 @@ use super::super::slice::directory::get_latest_slices_from_slice_root_directory;
use super::super::slice::section::Kind;

pub struct MakeCommand<'a> {
pub layer: String,
pub layer: String,
pub os: String,
pub slice_root_directory: &'a Path
pub slice_root_directory: &'a Path,
}

impl<'a> MakeCommand<'a> {
fn find_main_slice(&self, slices: &'a Vec<Slice>) -> &'a Slice {
slices.iter().find(|slice| {
slice.name.contains(&self.layer) && slice.get_os_list().contains(&self.os)
}).unwrap()
}

fn add_code_for_slice(current_code: &mut String, slice: &Slice, available_slices: &mut Vec<&Slice>) {
if let Some(dep_section) = slice.sections.iter().find(|section| section.kind == Kind::Dep) {
for dependency in &dep_section.items {
let dependency_position = available_slices.iter().position(|slice| slice.name == *dependency);
if let Some(dependency_position) = dependency_position {
let dependency = available_slices.remove(dependency_position);
MakeCommand::add_code_for_slice(current_code, &dependency, available_slices);
}
}
}
let run_section = slice.sections.iter().find(|section| section.kind == Kind::Run).unwrap();
for item in &run_section.items {
current_code.push_str(&item);
current_code.push('\n');
}
}

fn get_code_for_latest_slice(&self) -> Result<String, String> {
match get_latest_slices_from_slice_root_directory(&self.slice_root_directory) {
Ok(slices) => {
let mut available_slices: Vec<&Slice> = Vec::new();
for slice in &slices {
available_slices.push(&slice);
}
let main_layer = self.find_main_slice(&slices);
let mut string = String::new();
MakeCommand::add_code_for_slice(&mut string, &main_layer, &mut available_slices);
Ok(string)
},
Err(error) => Err(error)
}
}
fn find_main_slice(&self, slices: &'a Vec<Slice>) -> &'a Slice {
slices.iter()
.find(|slice| {
slice.name.contains(&self.layer) && slice.get_os_list().contains(&self.os)
})
.unwrap()
}

fn add_code_for_slice(current_code: &mut String,
slice: &Slice,
available_slices: &mut Vec<&Slice>) {
if let Some(dep_section) = slice.sections.iter().find(|section| section.kind == Kind::Dep) {
for dependency in &dep_section.items {
let dependency_position = available_slices.iter().position(|slice| {
slice.name == *dependency
});
if let Some(dependency_position) = dependency_position {
let dependency = available_slices.remove(dependency_position);
MakeCommand::add_code_for_slice(current_code, &dependency, available_slices);
}
}
}
let run_section = slice.sections.iter().find(|section| section.kind == Kind::Run).unwrap();
for item in &run_section.items {
current_code.push_str(&item);
current_code.push('\n');
}
}

fn get_code_for_latest_slice(&self) -> Result<String, String> {
match get_latest_slices_from_slice_root_directory(&self.slice_root_directory) {
Ok(slices) => {
let mut available_slices: Vec<&Slice> = Vec::new();
for slice in &slices {
available_slices.push(&slice);
}
let main_layer = self.find_main_slice(&slices);
let mut string = String::new();
MakeCommand::add_code_for_slice(&mut string, &main_layer, &mut available_slices);
Ok(string)
}
Err(error) => Err(error),
}
}
}

impl<'a> Command for MakeCommand<'a> {
fn run(&mut self) {
match self.get_code_for_latest_slice() {
Ok(code) => println!("Code = {}", code),
Err(error) => println!("Error = {}", error)
}
}
fn run(&mut self) {
match self.get_code_for_latest_slice() {
Ok(code) => println!("Code = {}", code),
Err(error) => println!("Error = {}", error),
}
}
}

#[test]
fn test_make_command() {
let expected_code = "apt-get update -y
apt-get install libc6-dev libssl-dev make build-essential libssl-dev libreadline6-dev zlib1g-dev libyaml-dev libz-dev -y
let expected_code = "apt-get update -y
apt-get install libc6-dev libssl-dev make \
build-essential libssl-dev libreadline6-dev zlib1g-dev libyaml-dev \
libz-dev -y
apt-get upgrade -y
apt-get install wget -y
cd /tmp
wget https://cache.ruby-lang.org/pub/ruby/2.2/ruby-2.2.3.tar.gz
tar xvzf ruby-2.2.3.tar.gz
wget \
https://cache.ruby-lang.org/pub/ruby/2.2/ruby-2.2.3.tar.gz
tar xvzf \
ruby-2.2.3.tar.gz
cd ruby-2.2.3
./configure --prefix=/usr/local
make
make install
\
make install
cd ..
wget https://rubygems.org/rubygems/rubygems-2.4.8.tgz
tar xvzf rubygems-2.4.8.tgz
\
tar xvzf rubygems-2.4.8.tgz
cd rubygems-2.4.8
ruby setup.rb
gem install jekyll -v '3.0.0.pre.beta9'
gem install \
jekyll -v '3.0.0.pre.beta9'
";
let slice_root_directory = get_slice_root_directory();
let command = MakeCommand { layer: "jekyll".to_string(), os: "debian".to_string(), slice_root_directory: &slice_root_directory };
let returned_code = command.get_code_for_latest_slice().unwrap();
assert_eq!(returned_code, expected_code);
}
let slice_root_directory = get_slice_root_directory();
let command = MakeCommand {
layer: "jekyll".to_string(),
os: "debian".to_string(),
slice_root_directory: &slice_root_directory,
};
let returned_code = command.get_code_for_latest_slice().unwrap();
assert_eq!(returned_code, expected_code);
}
2 changes: 1 addition & 1 deletion impl/rust/src/commands/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ mod helper;
pub mod command;
pub mod fetch_command;
pub mod find_command;
pub mod make_command;
pub mod make_command;
24 changes: 12 additions & 12 deletions impl/rust/src/for_testing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ use std::env::current_dir;
use std::path::PathBuf;

pub fn get_slice_root_directory() -> PathBuf {
let mut slice_root_directory = current_dir().unwrap();
slice_root_directory.push("target");
let build_type = if cfg!(debug_assertions) {
"debug"
} else {
"release"
};
slice_root_directory.push(build_type);
slice_root_directory.push(".sb");
slice_root_directory.push("slices");
slice_root_directory.to_path_buf()
}
let mut slice_root_directory = current_dir().unwrap();
slice_root_directory.push("target");
let build_type = if cfg!(debug_assertions) {
"debug"
} else {
"release"
};
slice_root_directory.push(build_type);
slice_root_directory.push(".sb");
slice_root_directory.push("slices");
slice_root_directory.to_path_buf()
}
Loading