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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
use anyhow::Result;
use futures::stream::{Stream, StreamExt};
use std::{
    collections::{HashMap, HashSet},
    path::{Path, PathBuf},
    sync::{
        atomic::{AtomicUsize, Ordering},
        Arc, Mutex,
    },
};
use structopt::StructOpt;
use tokio::sync::mpsc;
use tokio_stream::wrappers::ReceiverStream;
use warp::Filter;

static NEXT_SUB_ID: AtomicUsize = AtomicUsize::new(1);

#[derive(Debug, StructOpt)]
enum Arguments {
    Publish(Publish),
    Serve(Serve),
}

#[derive(Debug, StructOpt)]
struct Publish {
    #[structopt(long)]
    manifest_path: Option<PathBuf>,
}

#[derive(Debug, StructOpt)]
struct Serve {
    #[structopt(long, short, default_value = "3000")]
    port: u16,

    #[structopt(long)]
    manifest_path: Option<PathBuf>,
}

type Subscriptions = Arc<Mutex<HashMap<usize, mpsc::Sender<String>>>>;

#[tokio::main]
pub async fn main() {
    let args = Arguments::from_args();
    match args {
        Arguments::Publish(args) => publish(args),
        Arguments::Serve(args) => serve(args).await,
    }
    // TODO better error message
    .unwrap()
}

fn publish(publish: Publish) -> Result<()> {
    let mut compiler = Compiler::new(publish.manifest_path.as_deref())?;

    compiler.multitrack = false;
    compiler.compile()?;

    compiler.multitrack = true;
    compiler.compile()?;

    // TODO generate html

    Ok(())
}

async fn serve(serve: Serve) -> Result<()> {
    let subscriptions = Subscriptions::default();

    let filter_subs = subscriptions.clone();
    let subs_filter = warp::any().map(move || filter_subs.clone());

    let project = warp::path("_updates")
        .and(warp::get())
        .and(subs_filter)
        .map(|subs| warp::sse::reply(warp::sse::keep_alive().stream(Subscriber::new(subs))));

    let compiler = Compiler::new(serve.manifest_path.as_deref())?;

    let files = warp::path("euphony").and(warp::fs::dir(compiler.root.join("target/euphony/")));

    let routes = files
        .or(project)
        .with(warp::cors().allow_any_origin().allow_method("GET"));

    std::thread::spawn(move || watcher::create(subscriptions, compiler));

    warp::serve(routes).run(([0, 0, 0, 0], serve.port)).await;

    Ok(())
}

struct Subscriber {
    subs: Subscriptions,
    recv: ReceiverStream<String>,
    id: usize,
}

impl Subscriber {
    pub fn new(subs: Subscriptions) -> Self {
        let (send, recv) = mpsc::channel(10);

        let id = NEXT_SUB_ID.fetch_add(1, Ordering::SeqCst);
        subs.lock().unwrap().insert(id, send);

        let recv = ReceiverStream::new(recv);

        Self { subs, recv, id }
    }
}

impl Stream for Subscriber {
    type Item = Result<warp::sse::Event, core::convert::Infallible>;

    fn poll_next(
        mut self: core::pin::Pin<&mut Self>,
        cx: &mut core::task::Context<'_>,
    ) -> core::task::Poll<Option<Self::Item>> {
        if let Some(path) = futures::ready!(self.recv.poll_next_unpin(cx)) {
            let event = warp::sse::Event::default().data(path);
            Some(Ok(event)).into()
        } else {
            None.into()
        }
    }
}

impl Drop for Subscriber {
    fn drop(&mut self) {
        self.subs.lock().unwrap().remove(&self.id);
    }
}

mod watcher {
    use super::*;
    use notify::{watcher, DebouncedEvent, RecursiveMode, Watcher};
    use std::{collections::HashSet, sync::mpsc::channel, time::Duration};

    pub(crate) fn create(subs: Subscriptions, mut compiler: Compiler) {
        let _ = compiler.compile();

        let (tx, rx) = channel();

        let mut watcher = watcher(tx, Duration::from_millis(100)).unwrap();

        watcher
            .watch(&compiler.root, RecursiveMode::Recursive)
            .unwrap();

        loop {
            let mut updates = HashSet::new();

            fn map_event(event: DebouncedEvent) -> Option<PathBuf> {
                use DebouncedEvent::*;
                match event {
                    Create(path) | Write(path) | Chmod(path) | Rename(_, path) => Some(path),
                    _ => None,
                }
            }

            let mut should_compile = false;
            let mut should_rebuild_manifest = false;

            if let Ok(event) = rx.recv() {
                if let Some(path) = map_event(event) {
                    should_rebuild_manifest |= path.ends_with("Cargo.toml");
                    if !path.components().any(|c| c.as_os_str() == "target") {
                        should_compile = true;
                    }

                    if path.ends_with("project.json") {
                        updates.insert(path);
                    }
                }

                while let Ok(event) = rx.try_recv() {
                    if let Some(path) = map_event(event) {
                        should_rebuild_manifest |= path.ends_with("Cargo.toml");
                        if !path.components().any(|c| c.as_os_str() == "target") {
                            should_compile = true;
                        }

                        if path.ends_with("project.json") {
                            updates.insert(path);
                        }
                    }
                }
            }

            should_compile |= should_rebuild_manifest;

            for update in updates.drain() {
                // notify websockets of project updates
                let path = update
                    .strip_prefix(compiler.root.join("target"))
                    .unwrap()
                    .display()
                    .to_string();
                let subs = subs.lock().unwrap();
                for sub in subs.values() {
                    let _ = sub.blocking_send(path.clone());
                }
            }

            if should_rebuild_manifest {
                let _ = compiler.rebuild_manifest();
            }

            if should_compile {
                let _ = compiler.compile();
            }
        }
    }
}

#[derive(Debug)]
struct Compiler {
    pub root: PathBuf,
    pub projects: HashSet<String>,
    pub multitrack: bool,
}

impl Compiler {
    pub fn new(manifest_path: Option<&Path>) -> Result<Self> {
        let mut projects = HashSet::new();

        let root = Self::build_manifest(manifest_path, &mut projects)?;

        let comp = Self {
            root,
            projects,
            multitrack: true,
        };
        Ok(comp)
    }

    pub fn rebuild_manifest(&mut self) -> Result<()> {
        let manifest_path = self.root.join("Cargo.toml");
        Self::build_manifest(Some(&manifest_path), &mut self.projects)?;
        Ok(())
    }

    fn build_manifest(
        manifest_path: Option<&Path>,
        projects: &mut HashSet<String>,
    ) -> Result<PathBuf> {
        let mut cmd = cargo_metadata::MetadataCommand::new();
        if let Some(manifest_path) = manifest_path {
            cmd.manifest_path(manifest_path);
        }
        let meta = cmd.exec()?;

        projects.clear();

        for id in meta.workspace_members.iter() {
            let package = &meta[id];
            for target in package.targets.iter() {
                if target.kind.iter().any(|k| k == "bin")
                    && package.dependencies.iter().any(|dep| dep.name == "euphony")
                {
                    projects.insert(package.name.clone());
                }
            }
        }

        Ok(meta.workspace_root.into())
    }

    pub fn compile(&self) -> Result<()> {
        let status = std::process::Command::new("cargo")
            .arg("build")
            .current_dir(&self.root)
            .spawn()?
            .wait()?;

        if !status.success() {
            eprintln!("cargo build failed");
            return Err(anyhow::anyhow!("build command failed"));
        }

        for project in &self.projects {
            let mut proc = std::process::Command::new(format!("target/debug/{}", project));
            proc.arg("render");
            if self.multitrack {
                proc.arg("--multitrack");
            }

            let status = proc.current_dir(&self.root).spawn()?.wait()?;

            if !status.success() {
                eprintln!("{:?} failed", project);
            }
        }

        Ok(())
    }
}