Gulp minify and move files with same folder structure - gulp

I have a folder structure like this:
.
├── dist/
└── src/
├── index.php
└── projects/
├── some-project/
│ ├── main.css
│ ├── main.js
│ └── index.html
└── N'th project/ ...
I need to minify and move every project inside src/ to dist/ without breaking folder structure. Should look like this:
.
├── dist/
│ ├── index.php
│ └── projects/
│ ├── some-project/
│ │ ├── main.css(minified)
│ │ ├── main.js(minified)
│ │ └── index.html
│ └── N'th project/ (also minified) ...
└── src/
├── index.php
└── projects/
├── some-project/
│ ├── main.css
│ ├── main.js
│ └── index.html
└── N'th project/ ...
Thanks for your time.

I solved it with something like this. If any one want to use:
.pipe(gulp.dest(function(file){
let splittedUrl = file.history[0].split("\\");
let projectName = splittedUrl.slice(splittedUrl.indexOf("projects"),-1);
//console.log(projectName.join("/"));
return "dist/" + projectName.join("/");
}));

gulp.task("projectTest", () => {
return gulp.src("./src/projects/**/*.css")
// minify pipe, etc.
.pipe(gulp.dest('dist/projects'))
});
From https://gulpjs.com/docs/en/api/concepts#glob-base:
A glob base - sometimes called glob parent - is the path segment
before any special characters in a glob string. As such, the glob base
of /src/js/**.js is /src/js/. All paths that match the glob are
guaranteed to share the glob base - that path segment can't be
variable.
Vinyl instances generated by src() are constructed with the glob base
set as their base property. When written to the file system with
dest(), the base will be removed from the output path to preserve
directory structures.
That is a little confusing as written. In my code the part before the globstar ** is the base. So ./src/projects/ is the base. And the directory structure under the base is always preserved - but that wouldn't include projects (because that is part of the base and will be removed automatically in gulp.dest).
In the dest pipe, the directory structures (project1, project2, etc.) are still there but would be sent to your dest folder without the base path at the beginning. So you will need to add whatever part of that base path back into your dest folder if you want it.
So gulp.dest('dist/projects') to get the preserved folder structure put back under projects as you want.
So if what I said is true, this alternative should work as well:
gulp.task("projectTest", () => {
// return gulp.src("./src/projects/**/*.css")
return gulp.src("./src/**/*.css")
// .pipe(gulp.dest('dist/projects'))
.pipe(gulp.dest('dist'))
});
Now the directory structure under src is preserved (becasue src is the base). So you get the same result just sending all that to dist.
You can decide which form you prefer, I think the first version is a little clearer. And if there are files under src that you do not want captured by the gulp.src glob.

Related

Flask: Efficient on-demand delivery of selected 2D slices from 3D volumes

How can I efficiently store hundreds of 3D volumes and serve selected instance slices on-demand with Flask?
I am currently solving the issue by saving each subvolume as an image collection of its slices. Here each folder is a subvolume (cropped out object) - the image names correspond to the slices of the volume from which the subvolume was cropped.
├── 1/
│ ├── 1.png
| ...
│ ├── 10.png
│ └── 11.png
├── 2/
│ ├── 0.png
| ...
│ ├── 4.png
│ └── 5.png
├── 3/
│ ├── 3.png
| ...
│ ├── 11.png
│ └── 12.png
...
|
└── 176/
├── 34.png
...
├── 43.png
└── 44.png
Instead, I would like to store each subvolume, e.g., as a single tiff file and only serve the required slices.
├── 1/
│ └── image.tif
├── 2/
│ └── image.tif
├── 3/
│ └── image.tif
...
|
└── 176/
└── image.tif
However, according to this Stackoverflow answer the only mainstream browser that supports tiff is Safari.
Is there a way where I don't have to save every single image? Especially considering that I will only need to depict the most centric slice of a subvolume in most cases and only in a few cases make the whole subvolume browsable.

Import and convert module into qtdesign

I would like to integrate the python modules (in the algo/ folder) into the main program that uses an interface_ui is created by QT designer.
This is my folder structure.
├── algo/
│ └── main_algo.py
│ └── a.py
│ └── b.py
│ └── read_data.py
│ └── data/
│ └── data.bin
└── main.py
└── interface_ui.py
Note that the main_algo.py will import a.py, b.py, and read_data.py (function to read data.bin)
So, I have two solutions:
I have created a button in the interface and executed the function main_algo.py each time I push the button
exec(open("path/main_algo.py.py").read())
I would convert all the modules inside the algo/ folder into the function and call back the function in the main.py whenever I want.
import sys
sys.path.insert(1,"path_to_algo/")
from a import x,y
from b import z
import read_data
Please tell me the advantages of each solutions and how can we do the second solution.
Thank you very much

finding path in HTML

My file is organized like this:
My/
├── public/
│ ├── css/
│ │ ├── sign.css
├── views/
│ ├── register.ejs
And I typed in below to access the "sign.css" file:
<link href="css/sign.css" rel="stylesheet">
Why is the href path correct?
Shouldn't it be ../public/css/sign.css
since in order to access sign.css, the step is
"go back one file to My -> go into public -> go into css -> access sing.css"?
app.use(express.static('public'))
Above code sets public directory from which to serve static assets.
Express looks up the files relative to the static directory, so the name of the static directory is not part of the URL.

Modify path of gulp.dest

My goal is to find the binaries in here, and elevate them to the first subdir in exe dir.
My simplified (in reality it has more junk, more subdirs) folder structure is this:
src
├── exe
│ └── nix
│ └── binary.out
│ └── blah
│ └── mac
│ └── binary.out
│ └── blah
│ └── win
│ └── src
│ └── trigger
│ └── trigger
│ └── trigger
│ └── binary.exe
│ └── Debug
My goal is to copy just the binary files into dist/exe like this:
dist
├── exe
│ └── nix
│ └── binary.out
│ └── mac
│ └── binary.out
│ └── win
│ └── binary.exe
This is the code I use right now:
var path = require('path');
gulp.src(['src/exe/**/*.out', 'src/exe/**/*.exe'])
.pipe(gulp.dest(function(file) {
var os = file.path.match(/(?:win|mac|nix)/)[0];
return 'dist/exe/' + os + '/' + path.basename(file.path);
}));
However this is still copying the structure even after the os name. It's like it appends the **/ stuff after my returned string. Is there any way to make it not do this?
It like appends the **/ stuff after my returned string. Is there anyway to make it not do this
Yep, that's exactly what it does and it's intentional. Anything before the ** is treated as the base path, anything after is the path that is used when writing the file to the destination directory. (See this answer if you want to know more).
Normally you could use the base option in gulp.src() to change this, but this doesn't really work in your case since you would need one base path for nix/mac and another one for win.
On top of all that you're using gulp.dest() wrong. gulp.dest() specifies the destination directory. You're trying to use it to specify a destination file. That's not possible.
The easiest solution for you is to use gulp-rename:
var path = require('path');
var rename = require('gulp-rename');
gulp.src(['src/exe/**/binary.{exe,out}'])
.pipe(rename(function(file) {
file.dirname = file.dirname.split(path.sep)[0];
}))
.pipe(gulp.dest('dist/exe'));

An empty string with File-IO in VoltRb

I'm trying to do some file-reading in VoltRb and embed the contents of those files in my views. I initially planned that I would read the files on the server-side (since the client-side doesn't support file-reading), and then pass the strings of those files on to the client, but it seems that I'm getting an empty string for my files on the client.
Here is some code to demonstrate my problem:
file.txt
file contents
main_controller.rb
if RUBY_ENGINE == 'ruby'
file = File.open("app/files_to_read/lib/file.txt", "r")
$file_text = file.read
file.close
puts $file_text # returns "file contents" on the server side
puts $file_text.class # returns "String" on the server side
end
module Main
class MainController < Volt::ModelController
model :page
def index
page._text = $file_text
puts $file_text # returns an empty string in the browser console
end
...
end
end
index.html
<:Title>
Home
<:Body>
<h1>Home</h1>
<p>{{ _text }}</p>
<!-- ^ an empty string -->
My directory tree looks like this:
app
├── files_to_read
│   └── lib
│   └── file.txt
└── main
├── assets
├── config
├── controllers
│   └── main_controller.rb
├── lib
├── tasks
└── views
└── main
├── about.html
├── index.html
└── main.html
Why am I getting an empty string for my file and how do I fix this?
So one thing to keep in mind is that controllers will be run twice, once on the client, and once on the server. So the client one will be a different instance than the ones on the server, so they won't have access to the $file_text global.
What you can do though is create a task that reads the file and then returns the text of the file. Check out #RickCarlino's excellent tasks tutorial video: http://datamelon.io/blog/2015/creating-volt-task-objects.html
Thanks