I'm looking at this example which uses deployment manager to deploy a cloud function: https://github.com/GoogleCloudPlatform/deploymentmanager-samples/tree/master/examples/v2/cloud_functions
One of the things that makes this unwieldy and not very usable is this, it requires you explicitly import EVERY file in the function:
imports:
- path: cloud_function.py
# The function code will be defined for the files in function/
- path: function/index.js
- path: function/package.json
It's not acceptable to have to add to that every time there is a new file. Deployment Manager also does not support wildcards.
How can you import files programmatically?
This is the part of cloud_function.py that references the imported files, I tried to just use a string but it seems that import actually copies files somewhere? How do I do that programmatically so I don't need to explicitly define every individual file?
files = ["function/index.js","function/package.json"] # this does not work if these files have not been declared via "import"
#for imp in ctx.imports:
for imp in files:
if imp.startswith(ctx.properties['codeLocation']):
zip_file.writestr(imp[len(ctx.properties['codeLocation']):],
ctx.imports[imp])
You have to turn on globbing, in this way config files can now use glob patterns in import paths:
gcloud config set deployment_manager/glob_imports True
Here are examples where entire folders can be added:
imports:
- path: templates/simple_frontend.py
name: simple_frontend.py
# Helper classes
- path: helper/*.py
# Configuration files
- path: configs/*.yaml
- path: configs/*/*.yaml
The full documentation can be found here.
Related
I am looking at adding a new javascript library to drupal 8. I want the ability to call this library from other modules / libraries.
The way WordPress does this is by the use of wp_enqueue_scripts. Using a common library would make sure that my library is loading just once.
An existing implementation of this in drupal is the way you can add dependencies into your the yml files
js:
dependencies:
- core/jquery
Is there a way I can add my own javascript library as the dependency that can be used throughout my drupal instance (all modules and themes should be able to use this as a dependency)
If you want to add any custom code, you need to create and enable your custom module or theme, where you can define your custom javascript file in the my_module.libraries.yml file, like this:
my_library:
js:
dist/js/my_stuff.js: {}
dependencies:
- core/jquery
In this case, your custom code is placed in de dist/js folder.
Then you should add your library to Drupal. I usually use the my_module.info.yml to define my custom libraries in this way:
libraries:
- my_module/my_library
You can also use your custom library as a dependency:
my_other_library:
js:
dist/js/my_other_stuff.js: {}
dependencies:
- core/jquery
- my_module/my_library
More info about attaching libraries:
https://www.drupal.org/docs/8/creating-custom-modules/adding-stylesheets-css-and-javascript-js-to-a-drupal-8-module
More info about *.info.yml file:
https://www.drupal.org/docs/8/theming-drupal-8/defining-a-theme-with-an-infoyml-file
I'm working on a system where I can't add a new module by adding it's path to sys.path. Instead, I want to place the module in the same folder as the files using it, and then import the module on runtime using imp or importlib (or similar).
I've tried to use both imp and importlib, but can not get it to work. Time will tell if I'm just misinterpreting how and what params to specify when using either of the two libraries.
The folder structure for my project is defined like this:
root-folder-in-sys-path/
- file1.py
- file2.py
- file3.py
- my-module/
--- __init__.py
--- helper1.py
--- helper2.py
As my example indicates, the root folder is part of sys.path. The files (file1.py etc.) is part of the system and is from where I need access to the module. Only files that contains classes of a specific type is added, so it will not be possible just to add the module files in the root to load them, as they will be ignored. Best case would be if helper1.py to helper-n.py is made available - otherwise It is ok if only one is loaded.
Thanks.
I was able to come up with a solution that loads anyone of the helpers. I guess it could easily be made into a package with many more modules this way as well.
To for example access helper1.py in file1.py, this will work:
import os.path, imp
path = os.path.abspath(os.path.join(os.path.dirname(__file__), "my-module/helper1.py"))
im = imp.load_source("helper1", path)
If you find a better solution, then please let me know!
I have an ember app, and a folder with a file playGame/game.js. This file includes game logic, and I want to import it for asset compilation.
If this file is under app/playGame/game.js and my Brocfile is like this:
app.import('app/playGame/game.js')
this gives the error, path or pattern app/playGame/game.js didn't match any files..
but if I put the file under bower_components/playGame/game.js and my Brocfile:
app.import('bower_components/playGame/game.js'), this compiles successfully.
What is the problem and solution here?
There are two parts to this:
Where should I put my file to import it as an asset?
Why isn't putting it in my app-folder working?
The way to do what you want is to create a folder called vendor in your root, put the file somewhere in there, and then import it in your Brocfile.js like so:
app.import('vendor/playGame/game.js');
This is documented on ember-cli.com, although somewhat hidden.
You could also put it in bower_components, but that folder is for things installed with bower, and could theoretically be deleted (in fact, this is a common recommendation to various issues). Things in bower_components is also not checked in to version control by default, which you probably want to do in this case.
This should solve your issue.
Now, why doesn't it work to put it in /app?
app is a special folder. From the documentation:
Contains your Ember application’s code. Javascript files in this
folder are compiled through the ES6 module transpiler and concatenated
into a file called app.js.
This is what makes it possible for you to import stuff from within your app. The folders in app is available directly under your <appname> namespace, along with some other files and folders like config/environment.
Example:
import myWidget from 'my-app/widgets/my-widget';`
The referenced file is /app/widgets/my-widget.js.
The ember-cli website has some more resources for how to use modules. Read those if this doesn't make any sense.
To sum up:
You could put your file in app, but that would make it part of your transpiled package, and you'd have to use it that way internally with an export and everything else that comes with it. It would end up as part of <appname>.js
You could put your file in vendor and import it in your Brocfile.js as explained above. It would be part of vendor.js and load before your app code.
Is it somehow possible to import a folder structure into a resources file?
I have folders such as:
- JS
- Lots of JS files
- Images
- Lots of images
etc.
I can only seem to import one file at a time, I have around 80 files.
Any ideas?
I am not aware of anything, but this python script should give you some idea for a quick'n dirty workaround:
main.py
import os
print('<RCC>')
print('<qresource prefix="/">')
for root, dirs, files in os.walk(sys.argv[1]):
for name in files:
print('<file>%s</file>' % name)
print('</qresource>')
print('/RCC')
Then, you can invoke the script as follows:
python main.py myfolder
Diclaimer: the code does not check against len(sys.argv), etc, but it would be pretty simple to extend and hand-craft it further. It does not write into a dedicated file either, but you can redirect the output, and so on.
I'm new to Intern and trying to figure out how to configure it for our project. Our file hierarchy is not exactly the same as the examples in the intern-tutorial or readme for intern on github. I think I have the package locations correctly specified as it does not complain about not finding the test module. It even seems to run the test I have setup but it then tries to run tests on the rest of the modules defined in my package module being targeted. It first tries to load .../dojo/_base/declare.js. So I tried to specify the excludeInstrumentation property value.
I specified it as:
excludeInstrumentation: /^(?:dojo|dijit|dgrid|tests)\//
but it doesn't exclude it. My target module has this in the define:
define([
'dojo/_base/declare',
'dojo/_base/lang',
'dojo/dom-construct',
'dojo/on',
'dojo/query',
...
'dijit/layout/BorderContainer',
'dijit/layout/ContentPane',
'dijit/form/TextBox',
...
'dgrid/OnDemandGrid',
'dgrid/Keyboard',
...
But I get errors:
node node_modules/intern/client.js config=tests/intern
Defaulting to "console" reporter
Error: Failed to load module dojo/_base/declare from
/home/bholm/Projects/src/sandbox/dojo/_base/declare.js
(parent: ev/grids/FilterGrid)
at /home/bholm/Projects/src/sandbox/node_modules/intern/node_modules/dojo/dojo.js:742:12
at fs.js:207:20
at Object.oncomplete (fs.js:107:15)
I should note that the dojo, dijit and dgrid packages are actually located in:
/home/bholm/Projects/src/sandbox/libs/dojo/... (notice the addition of libs in the path).
I forgot to add my loader property in the config file intern.js:
loader: {
//baseUrl: '..',
packages: [
{ name: 'intern', location: 'node_modules/intern' },
{ name: 'ev', location: 'web/libs/ev' }
]
},
Any ideas on why the regex is not excluding?
Do not put the intern package in your loader configuration. Only put application-specific configuration in the loader configuration.
excludeInstrumentation is only to prevent your scripts from being modified with code coverage data when passed through the Intern proxy. It does not change the way the loader works, or stop your AMD dependencies from being requested and loaded normally.
If your application uses 3rd party packages (dojo, dijit, etc.) that are not directly within baseUrl, you need to make sure that they are configured in packages, just like they need to be configured when running the actual application.