Skip to content

Commit

Permalink
bump: version 0.2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Kilerd committed Aug 11, 2020
1 parent d0b5bb3 commit 06ca99b
Show file tree
Hide file tree
Showing 11 changed files with 70 additions and 124 deletions.
3 changes: 3 additions & 0 deletions .cargo/config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[alias]
fc = "fmt --all -- --check"
cc = "clippy --all-targets -- -D warnings"
97 changes: 11 additions & 86 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ authors = ["Kilerd Chan <[email protected]>"]
license = "MIT"
edition = "2018"


[dependencies]
structopt = "0.3.3"
file-lock = "1.1.20"
tera = "0.11.20"
serde = "1.0.101"
serde_derive = "1.0.101"
Expand All @@ -32,3 +32,4 @@ regex = "1.3.9"
itertools = "0.9.0"
serde_json = "1.0.56"
env_logger = "0.7.1"
fs2 = "0.4.3"
Empty file added Staple.lock
Empty file.
30 changes: 1 addition & 29 deletions homepage/public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ <h1 class="title">Staple</h1>
]
},
&quot;develop&quot;: {
&quot;live_reload&quot;: &quot;&lt;script&gt;\n\n function websocket_connect() {\n let webSocket = new WebSocket(\&quot;ws:&#x2F;&#x2F;localhost:8000&#x2F;notifier\&quot;);\n webSocket.onopen = function () {\n webSocket.send(\&quot;Connecting...\&quot;);\n console.log(\&quot;ws connection...\&quot;);\n };\n webSocket.onmessage = function (event) {\n let data = event.data;\n console.log(\&quot;ws get event: \&quot;, data);\n if (data === \&quot;refresh\&quot;) {\n window.location.reload();\n }\n };\n webSocket.onclose = function (e) {\n console.log(\&quot;ws disconnected.\&quot;, e);\n console.log(\&quot;trying to reconnect servere 5 second later\&quot;);\n setTimeout(websocket_connect, 5000);\n };\n }\n\n if (\&quot;WebSocket\&quot; in window) {\n websocket_connect();\n\n } else {\n console.log(\&quot;your browser does not support websocket\&quot;);\n }\n&lt;&#x2F;script&gt;&quot;
&quot;live_reload&quot;: &quot;&quot;
},
&quot;page&quot;: {
&quot;content&quot;: {
Expand Down Expand Up @@ -128,33 +128,5 @@ <h1 class="title">Staple</h1>

</div>
</body>
<script>

function websocket_connect() {
let webSocket = new WebSocket("ws://localhost:8000/notifier");
webSocket.onopen = function () {
webSocket.send("Connecting...");
console.log("ws connection...");
};
webSocket.onmessage = function (event) {
let data = event.data;
console.log("ws get event: ", data);
if (data === "refresh") {
window.location.reload();
}
};
webSocket.onclose = function (e) {
console.log("ws disconnected.", e);
console.log("trying to reconnect servere 5 second later");
setTimeout(websocket_connect, 5000);
};
}

if ("WebSocket" in window) {
websocket_connect();

} else {
console.log("your browser does not support websocket");
}
</script>
</html>
8 changes: 1 addition & 7 deletions src/command/build.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
use crate::{app::App, command::StapleCommand, error::StapleError};
use file_lock::FileLock;

pub(crate) fn build(develop: bool) -> Result<(), StapleError> {
StapleCommand::check_config_file_exist()?;
let _file_lock = match FileLock::lock("Staple.lock", true, true) {
Ok(lock) => lock,
Err(err) => panic!("Error getting write lock: {}", err),
};

StapleCommand::lock_file()?;
App::load(develop)?.render()
}
13 changes: 12 additions & 1 deletion src/command/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ use structopt::StructOpt;

use crate::error::StapleError;

use crate::constants::STAPLE_CONFIG_FILE;
use crate::{
constants::{STAPLE_CONFIG_FILE, STAPLE_LOCK_FILE},
util::lock::LockFile,
};

pub mod add;
pub mod build;
Expand Down Expand Up @@ -73,6 +76,7 @@ impl StapleCommand {
}
}

#[inline]
fn config_file_exist() -> bool {
Path::new(STAPLE_CONFIG_FILE).exists()
}
Expand All @@ -84,4 +88,11 @@ impl StapleCommand {
Err(StapleError::ConfigNotFound)
}
}

fn lock_file() -> Result<LockFile, StapleError> {
let lock_file = LockFile::new(STAPLE_LOCK_FILE)?;
info!("Preparing to lock file...");
lock_file.lock_file()?;
Ok(lock_file)
}
}
1 change: 1 addition & 0 deletions src/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ pub const HEARTBEAT_INTERVAL: Duration = Duration::from_secs(5);
pub const CLIENT_TIMEOUT: Duration = Duration::from_secs(10);

pub const STAPLE_CONFIG_FILE: &str = "Staple.toml";
pub const STAPLE_LOCK_FILE: &str = "Staple.lock";

pub const DESCRIPTION_SEPARATOR: &str = "<!--more-->";

Expand Down
1 change: 1 addition & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ mod constants;
mod error;
mod server;
mod template;
mod util;

mod data;

Expand Down
37 changes: 37 additions & 0 deletions src/util/lock.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
use crate::error::StapleError;
use fs2::FileExt;
use std::{
fs::{File, OpenOptions},
ops::Deref,
path::Path,
};

pub struct LockFile(File);

impl Deref for LockFile {
type Target = File;

fn deref(&self) -> &Self::Target {
&self.0
}
}

impl LockFile {
pub fn new(path: impl AsRef<Path>) -> Result<LockFile, StapleError> {
let file = OpenOptions::new()
.read(false)
.write(true)
.create(true)
.open(path.as_ref())?;
Ok(LockFile(file))
}
pub fn lock_file(&self) -> Result<(), StapleError> {
Ok(self.0.lock_exclusive()?)
}
}

impl Drop for LockFile {
fn drop(&mut self) {
let _result = self.0.unlock();
}
}
1 change: 1 addition & 0 deletions src/util/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod lock;

0 comments on commit 06ca99b

Please sign in to comment.