Are there any conventions regarding project directory structure?
I have a project that was initialized with crystal init app [project-name]. Inside of that is the src directory which has the folders for modules and classes. However, now I want to add an ORM which will require a models directory. What conventions are there for laying this out?
Right now I've created a sub-directory inside of src called modules where classes and modules are going.
There is no direct answer here. It very much depends on the pattern you want to follow. If you are using a framework like lucky or amber, I would suggest referring to their documentation as both prefer a conventional (rather than configurable) approach. Both follow a rails'esque convention.
[root]
⌙ /src
⌙ /models
⌙ /controllers
⌙ /views
If you are implementing services DDD like modules, then I would suggest:
[root]
⌙ /src
⌙ /[service/module a]
⌙ /[service/module b]
Related
Is there a way to programmatically insert the name of my home directory into file paths in Leiningen's project.clj?
I run a Leiningen project on different machines, where I have different home directories. The project uses jar files that are not managed by Maven; I download 'em and put them in a directory that's relative to my home directory, or copy them into the Leiningen project. The second option works but is undesirable.
An easy way to use the first option--keeping the jar files somewhere else--is to add a soft link to the "somewhere else" directory in my Leiningen directory. That works, but the link has to be different on each machine, so I can't include the link file in the git repository, and I'd rather include everything in the git repo.
Isn't there a way to use environmental variables, or otherwise refer to my home directory, in my project.clj file? I've gone through the sample project file and have not found a solution, so far.
I thought I could just construct path strings at run time--what's in project.clj is just Clojure code, after all. Since hard-coding my home directory into project.clj works without any trouble:
:resource-paths [/Users/myhomedir/dist/mason/jar/mason.19.jar")]
I figured I could do this:
:resource-paths [(str (System/getenv "HOME") "/dist/mason/jar/mason.19.jar")]
However, Leiningen doesn't like this at all:
java.lang.IllegalArgumentException: No implementation of method: :as-file of protocol: #'clojure.java.io/Coercions found for class: clojure.lang.PersistentList
Changing [...] to (vector ...) gives the same error but with 'clojure.lang.Symbol` at the end.
In the name of repeatability you'd better have the jar installed in the local maven repository and have it added to the project.clj :dependencies so it's fetched from there. But you said those jars won't be managed by maven, so here we go:
defproject is a macro and it allows to use unquoting to do arbitrary evaluation. It does it by calling the internal fn unquote-project. So you can do the following:
:resource-paths [~(str (System/getenv "HOME") "/dist/mason/jar/mason.19.jar")]
I am severely confused about where to put my templates files and static files.
I understand absolute and relative paths just fine, but I can't seem to find any instructions that mirror the installation I have. I know this resembles other questions, but those answers aren't working. The video I watched to successfully build a simple app didn't put templates in the Project folder, which is where logic tells me they should be.
I have Python at:
C:\Python27
Django (v1.6.5) at:
C:\Python27\Lib\site-packages\django
I created a project "mysite" and an app called "films."
Project "mysite":
C:\Python27\Scripts\venv\mysite
and an App "films":
C:\Python27\Scripts\venv\mysite\films
The video I watched had me put my templates at:
C:\Python27\Lib\site-packages\django\contrib\admin\templates
But this seems completely stupid because the templates are outside of both the Project and the App.
Shouldn't I put a templates folder in the Project folder:
C:\Python27\Scripts\venv\mysite\templates
And then create subdirectories using the App name?
What files do I need to edit (and how) to tell Django where to find them?
Follow a similar process for static files (css, images)?
Like all frameworks, django offers great benefits if you follow some guidelines (and give up some control). The trick is to know what these guidelines are.
For templates:
If the template is not tied to a particular application, put it in a templates directory at the root of your project. Add the full path to this directory to TEMPLATE_DIRS.
All other templates should go in a directory called templates inside your application directory. So if you application is called myapp, templates for myapp will go in myapp/templates/
For static files:
For files related to specific applications, inside your application directory create a directory called static, then inside it a directory with the name of your application. So, if your application is called myapp, you would have myapp/static/myapp. Place all your static content for this application here; for example myapp/static/myapp/js/funky.js.
For static files that are generic, create a directory called assets (or static) in the root directory of your project. Add the full path to this directory to STATICFILES_DIRS.
By default, django will search all applications listed in INSTALLED_APPS, and add any templates and static directories to its search path for files. This is how, by default, the admin works without you having to configure anything.
If you chose to place your templates and static files in some other location, only then do you need to modify the TEMPLATE_DIRS and STATICFILES_DIRS settings. If all your templates and static assets are tied to applications, just creating the directories as mentioned above makes everything work.
If you are wondering why you need to create another directory under myapp/static/ to store your static files, this is more for portability. The collectstatic command is a simply "copy and replace" utility. It will overwrite all files in the STATIC_DIR location. This means that if two applications have some static file with the same name, they will be overwritten without warning. Adding a subdirectory keeps your application's static assets from being overwritten, because the exact path will be created.
Suppose you have two applications, app1 and app2, and both have a file named style.css in their respected directories:
app1/static/css/style.css
app2/static/css/style.css
When you run collectstatic, you'll end up with the following (assuming static is the name of your STATIC_DIR setting):
static/css/style.css
This may be the style.css from app1 or app2, the other cannot be determined (its actually based on the INSTALLED_APPS order). To prevent this, if you have:
app1/static/app1/css/style.css
app2/static/app2/css/style.css
Now, you'll end up with:
static/app1/css/style.css
static/app2/css/style.css
Both files will be preserved.
You also shouldn't put your code in your virtual environment directory. The virtual environment is not part of your source code, and placing your project in the same directory may cause problems later.
Create a single directory for your environments - I call mine envs (creative, I know). Create all your environments in this directory. Once you activate the environment, you can work in any directory in your system and your shell will be configured for that environment's Python.
Finally for the best, accurate, most up-to-date information - always refer to the django manual and the tutorial. Almost all other resources (even the often suggested djangobook.com) are outdated.
We want to automate the production of a Leiningen project tree
entirely from an org-mode babel file. We want to do this so that we
can also create beautiful, typeset documentation via
org-latex-export-to-pdf. We want no less than full literate
programming in Clojure from org-mode.
The following command:
$ lein new ex1
produces a tree that looks like this:
ex1
ex1/.gitignore
ex1/doc
ex1/doc/intro.md
ex1/project.clj
ex1/README.md
ex1/resources
ex1/src
ex1/src/ex1
ex1/src/ex1/core.clj
ex1/test
ex1/test/ex1
ex1/test/ex1/core_test.clj
We want to do the identical thing just by running
org-babel-tangle, and no more, in our org-mode buffer in
emacs.
A difficulty arises: whereas tangle is happy to produce
files in existing subdirectories like src and test, it seems reluctant to produce the subdirectories if they don't exist. That means we must
create the directory structure by some other means -- unless we can
get tangle to do it for us, and that's the subject of this
StackOverflow question.
There are six files in the directory structure created by Leiningen. I can remove them all and re-create them from my org-file with BEGIN_SRC blocks such as the following
#+BEGIN_SRC clojure :tangle ./ex1/src/ex1/core.clj
(ns ex1.core)
(defn foo
"I don't do a whole lot."
[x]
(println x "Hello, World!"))
#+END_SRC
Notice particularly the name of the subdirectory path
#+BEGIN_SRC clojure :tangle ./ex1/src/ex1/core.clj
All is well if our directory structure already exists. org-mode's tangle will
create or update all six files described above and create new files in any existing directory. We don't know how to
get tangle to produce the directories; it complains that there is no such
directory.
A copy of the desired .org file can be found here if you would like more details.
It is possible use the following header in the begin_src section,
:mkdirp yes
FYI There's now a lein project template for using org based projects:
https://github.com/thi-ng/thing-babel
I have been working with EmberJS online guides, and it has been somewhat of a hit or miss process. With Sproutcore2 I could generate the project directory structure via commandline command and be done with it. Well with Ember, it is rather manual. I would like to be able to set up a project's director structure where I am able to separate the model(s),view(s), and control(s) into their own folders. I have been trying to do so but require statements aren't importing correctly from MVC folders. I have downloaded the "todos" app, but it is no help, because the directory structure is not demonstrated as I had expected.
I would like to start with a simple "HelloWorld" application where "app" will be my facade, and I can then import my models,views, and controls from their folders which will be placed under mvc folder within the "HelloWorldApp" folder. Appreciate all your help.
HelloWorldApp (project directory)
->js (directory)
->lib (directory)
->ember-0.9.5
->ember-0.9.5.min
->jquery-1.6.1.min
->template (directory)
->main-view-template
->mvc (directory)
->model (directory)
->view (directory)
->control (directory)
->css (directory)
app (app.js)
index.htm
Take a look at the early alpha of the Ember build tools' template structure (based on Ember-Skeleton): https://github.com/emberjs/ember-gem/tree/master/lib/ember/templates/app. I'd imagine that something very close to this structure will be the default moving forward.
After trying out different approaches, I ended up declaring all my model(s), views(s), and controller(s) in the index file right after where the ember-0.9.5 is being called via javascript. Once I did that I was able to instantiate my model "Person" in "app".
Thx.
I'm trying to set up a simple clojure project, and I'm not sure how to load files between the project. I'm sure that the answer is in the documentation, but I can't find a simple answer any where and I'm not sure where to look.
Essentially, my directory looks like this:
Clojure/
clojure/
clojure.jar
other clojure files
clojure-contrib/
clojure-contrib.jar
other contrib files
project/
main.clj
utils.clj
And I want main.clj to be something like this:
(ns project.main
(:require project.utils))
(greet)
and utils.clj to be something like this:
(ns project.utils)
(defn greet [] (println "Hello, World!"))
But that fails with:
Exception in thread "main" java.io.FileNotFoundException: Could not locate project/utils__init.class or project/utils.clj on classpath: (main.clj:1)
When I attempt to run it. My classpath includes the top Clojure/ directory and both jars. I also tried putting the project/ directory in the classpath as well, with no luck.
How do you set up a simple clojure project?
You don't mention what your environment is (i.e. Emacs/SLIME/Swank, vim/Vimclojure), so I'm going to assume you are trying to invoke it from the command line.
You need to have your Clojure/ project directory in the classpath:
java -cp path/to/clojure.jar:path/to/clojure-contrib.jar:path/to/Clojure ...
Make sure to check that paths are correct relative to current working directory. It needs to point to the root of your namespace (i.e. if running in Clojure/, the path is .).
In fact, your project layout Works On My Machine(tm), with the exception that I have use instead of require (but you should've got a different error anyway if you got to the point when Clojure could find all your files).
This answer I posted to another question should hopefully give you an idea of how your filenames should relate to namespace names for things to work. However, since your question is "how to set up a simple Clojure project", the following is a better start:
Go to GitHub and grab Leiningen.
Follow the instructions in the README. You'll end up doing something like
$ lein new my-project
$ cd my-project
# ... edit project.clj ...
$ lein deps
Hack away! You'll need to put your files in the correct places. That will mean putting your source files in the directory tree rooted at my-project/src, with your core namespace most likely residing at my-project/src/my_project/core.clj. But really, I've explained all the details in the answer linked to above, so please read it (and do leave a comment if I missed something). :-)
Leiningen will take care of the basic project layout and setting up the classpath for a REPL / swank / nailgun for you (if you haven't yet come across the latter two, you will soon -- but that's a separate topic, the swank part of which I have covered to a certain degree e.g. in this SO answer), so hopefully you'll never need to deal with the java -cp ... nonsense by hand. (The swank-related answer I linked to in the last parenthetical remark has details on how to set up swank with the correct classpath from within Emacs.)