Store directory structure as template for reference

Hi,

I am writing a command line tool to create different directory
structures (multiple nested folders). As of now there will be 5 to 8
different directory structures and they may change over time. I am
therefore considering the option of having an directory ‘template’ in an
XML or JSON format which the script can reference.

This should bypass me having to hard code the directory structure into
the actual scripts which seems rather silly.

My Question:

– Is this something you would recommend or is there another way such
things are usually done?
– Is there a '‘tutorial’ or guide of something similar which I can refer
to?

Please note I am relative new to Ruby and command line tool development
so rather than looking for a direct answer I am hoping you can point me
in the right direction as to what I need to look at.

Thank you in advance,
sofus

Hi Carlo,

Thank you for the quick respond. I will have a look at the FileUtils and
JSON doc you pointed to.

– sofus

Subject: Store directory structure as template for reference.
Date: lun 03 mar 14 01:54:05 +0100

Quoting SMITH GREY ([email protected]):

I am writing a command line tool to create different directory
structures (multiple nested folders). As of now there will be 5 to 8
different directory structures and they may change over time. I am
therefore considering the option of having an directory ‘template’ in an
XML or JSON format which the script can reference.

This should bypass me having to hard code the directory structure into
the actual scripts which seems rather silly.

If you are familiar with either format, they are both a good choice -
both are well supported in Ruby. But then, once you have read in the
details from your XML or JSON file, it is up to you to code and debug
the procedure that builds the directory structure. Ruby offers good
tools for manipulating file system (type

ri FileUtils

for example), but I do not think there is something ready-made for
doing what you need to do.

For XML, have a look here:

http://www.germane-software.com/software/rexml/

(the library is included in MRI Ruby). For JSON type

ri JSON

Carlo

+1 for Robert, it depends!

Take a look at what Jim W. and folks has left to us. Perhaps it
fits
what you’re needing.

http://rake.rubyforge.org/doc/rakefile_rdoc.html#label-Directory+Tasks
Em 03/03/2014 13:34, “Robert K.” [email protected]
escreveu:

On Mon, Mar 3, 2014 at 1:54 PM, SMITH GREY [email protected] wrote:

I am writing a command line tool to create different directory
structures (multiple nested folders). As of now there will be 5 to 8
different directory structures and they may change over time. I am
therefore considering the option of having an directory ‘template’ in an
XML or JSON format which the script can reference.

What are you using the directory structure template for? Is this only
for creating those folders? Or are you doing more with it?

If the only think you want to do is to create directories you could
store the structure in a tar and untar it or write a series of “mkdir
-p” commands into script files.

If you tell a bit more about the intended purpose of the script we
might come up with better ideas.

Kind regards

robert

Smith Grey, maybe you are trying to write a tool like the Python
Templer (http://templer-manual.readthedocs.org/en/latest/index.html)
that is used to define new project structures including
directories and files. I think that Templer is better that Rake
directory tasks, because it allows to define code files with patterns
that can be replaced with Templer parameters.

I think that project skeletons must be defined as data. So how to
codify that data? An options is to use XML or JSON files, but may
be it is simpler to use directories as Templer do.

Regards,
Daniel

El Mon, 3 Mar 2014 14:02:01 -0300
“Abinoam Jr.” [email protected] escribi:

Hi All thank you for all the great feedback. Please see my comments
below:

@Robert :

To give a bit of back story to what I am doing : I work in a small
jewellery design studio and we work off a shared drive where we store
all our projects.

As of now I am looking to generate directories for our various shared
projects such as:

1: seasonal collections
2: bespoke work
3: customisations
4: wholesale orders

Each time we need to generate a new directory I would like to give it a
name which then replaces a variable in the folder names and also copy
over files such as maya, photoshop etc which I have already made.

An example part of such a directory would look like this:

*** Cut out of a collection directory ***

{collection name}
{collection name}_inspiration
{collection name}_artwork
Illustrator
In the illustrator folder I will need to copy over pre made illustrator
files
Photoshop
In the Photoshop folder I will need to copy over pre made photoshop
files
{collection name}_3D
{collection name}_maya
In the maya folder I will need to copy over pre made maya render files
files

I wanted to take the template route as it would allow me to make
corrections or new types of structures without having to go through the
codes every time - this would also allow my partner (less coding savvy)
to make templates without opening the ruby files or having to involve me
every time.

Also I can imagine I would do a small GUI to make the directory
structure templates so that you can drag-drop and reorder at will and
then just phase them in to the main script to use them as needed.

@Abinoam Jr :
Thanks for the rake file link I will have a read through over the
weekend as I am sure it can kick start my thinking about my own script.

@ Daniel :
I had a look at the Templer and yes it does look like it’s the same as I
need but as I am not too savvy on the matter I can’t see if I can lift
the concept directly but I would need to read their docs properly on the
weekend to see if this could kick start my work – thank you for passing
the link.

Robert K. wrote in post #1138808:

On Tue, Mar 4, 2014 at 8:00 PM, SMITH GREY [email protected] wrote:

So, you have different types of projects as indicated above. For each
type of project you have a directory tree with files that serves as
template. Did I get that right?

– That’s correct.

If so, the easiest solution to me seems to be to have a template
directory with a subdirectory for each project type. If you start a
new project you copy the appropriate template directory and rename it.

– I was thinking about doing the same but I sense it could remove some
of the overview which I would have by having it in a template file of
sort. Also I sense (and this is just me looking at how we work) we would
do different types of collection directories which is based of other
structures so managing that through a xml or json files seems easier.

So you intend to have folders which have names that depend on the
project and folders with fixed name at the same level. That does not
seem to make sense to me. Or are “Illustrator” and “Photoshop” nested
in “{collection name}_artwork”?

– Yes, sorry the formatting was removed when I posted the reply, The
illustrator and Photoshop folders will be nested under the {collection
name}_ artwork folder.

So the problem you are trying to solve is not creation of a new
project but creation of a new project template?

  • Yes I already have the directory structure but right now we are
    copying a ‘blank’ directory and then manually renaming the folders
    within which is what I want to get out of.

Kind regards

robert

So far I am leaning towards having the structures saved in a JSON file
which I can call upon when I run the command line tool. It seems cleaner
and I like the idea of abstracting it out of the code and it should also
be more portable.

It might seem like overkill right now since it’s just a few directories
but I can easily imagine we will need to do this for many more types of
directories and the sooner we lean on a standard the more time I can
focus on the additional tools we need such as archiving and all that
jazz.

But I really appreciate you reply and I will post some results once I
get to that stage.
Best,
sofus

On Tue, Mar 4, 2014 at 8:00 PM, SMITH GREY [email protected] wrote:

4: wholesale orders

Each time we need to generate a new directory I would like to give it a
name which then replaces a variable in the folder names and also copy
over files such as maya, photoshop etc which I have already made.

So, you have different types of projects as indicated above. For each
type of project you have a directory tree with files that serves as
template. Did I get that right?

If so, the easiest solution to me seems to be to have a template
directory with a subdirectory for each project type. If you start a
new project you copy the appropriate template directory and rename it.

Photoshop
In the Photoshop folder I will need to copy over pre made photoshop
files
{collection name}_3D
{collection name}_maya
In the maya folder I will need to copy over pre made maya render files
files

So you intend to have folders which have names that depend on the
project and folders with fixed name at the same level. That does not
seem to make sense to me. Or are “Illustrator” and “Photoshop” nested
in “{collection name}_artwork”?

I wanted to take the template route as it would allow me to make
corrections or new types of structures without having to go through the
codes every time - this would also allow my partner (less coding savvy)
to make templates without opening the ruby files or having to involve me
every time.

Also I can imagine I would do a small GUI to make the directory
structure templates so that you can drag-drop and reorder at will and
then just phase them in to the main script to use them as needed.

So the problem you are trying to solve is not creation of a new
project but creation of a new project template?

Kind regards

robert

On Wed, Mar 5, 2014 at 7:42 AM, Robert K.
[email protected] wrote:

  • inspiration

which I can call upon when I run the command line tool. It seems cleaner
jazz.
http://blog.rubybestpractices.com/
PMFJI at this late juncture, but this seems like the perfect job for a
generator with templates. I’m not speaking of making this a rails
project, but borrowing liberally the idea of generators from that.
Thor is an awesome tool for that, or you could use Rake/Make clones,
etc. It sort of depends on whether there is any variance in what gets
created when a new project is spun up.

OTOH, if it is a stock skeleton with directories underneath it that
doesn’t actually change from project to project (except maybe as new
stock starting points are introduced), just duplicating that doesn’t
take a ruby script at all.

On Wed, Mar 5, 2014 at 10:36 AM, SMITH GREY [email protected]
wrote:

  • Yes I already have the directory structure but right now we are
    copying a ‘blank’ directory and then manually renaming the folders
    within which is what I want to get out of.

I personally would remove the variable part from names and create a
parent directory with that name:

{collection name}

  • inspiration
  • artwork
    • Illustrator
    • Photoshop
  • 3D
  • maya

If you stick with the prefix naming you could still write a script
that changes all {collection name} to the real name.

So far I am leaning towards having the structures saved in a JSON file
which I can call upon when I run the command line tool. It seems cleaner
and I like the idea of abstracting it out of the code and it should also
be more portable.

I would prefer the directory structure because that is easier to
manipulate with standard OS tools.

It might seem like overkill right now since it’s just a few directories
but I can easily imagine we will need to do this for many more types of
directories and the sooner we lean on a standard the more time I can
focus on the additional tools we need such as archiving and all that
jazz.

I still fail to see the advantage of the JSON approach but then again
I am probably missing a lot of detail.

Kind regards

robert

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs