Exclude all except one subfolder for Rubocop - ruby

Let's say we have a folder called bin and it includes another subfolder bin/scripts besides other folders and top-level files.
We are looking to scan only the bin/scripts/**/* files and ignore everything else in the bin folder.
This pattern bellow excludes everything in that folder
AllCops:
TargetRubyVersion: 3.1.2
NewCops: enable
Exclude:
- "bin/**/*"

Use ERB to Template Your Exclusions
This is covered by the RuboCop documentation on including/excluding files when used in conjunction with the section on pre-processing using ERB. There might be a more elegant way to do this, but using ERB to add excluded items based on an inverted regular expression using Enumerable#grep_v on a Dir#glob just seems easiest to me.
This was tested with Ruby 3.1.2 and RuboCop v1.30.1, and "just works." Your mileage may vary with other versions or approaches.
AllCops:
TargetRubyVersion: 3.1.2
NewCops: enable
Exclude:
<% Dir.glob('bin/**/*').grep_v(%r{bin/scripts/}).each do |path| %>
- <%= path %>
<% end %>
With the template above in .rubocop.yml and a tree structure of:
.
├── bin
│   ├── bar.rb
│   ├── baz
│   │   └── quux.rb
│   └── scripts
│   └── wuuble
│   └── zub.rb
└── lib
└── foo.rb
RuboCop only checks:
lib/foo.rb
bin/scripts/wuuble/zub.rb

Related

Bash: automatically add a file to a Xcode project?

I am creating a script.sh file that creates a Test.swift file and adds it into a Xcode project. However, I would like to know if there is a way to add this file to Xcode (in the project.pbxproj file) from this script? Instead of doing it manually in Xcode (Add files to Project...).
Thank you
3/05 Update
I tried #Johnykutty answer, here is my current Xcode project before executing the ruby script:
I have already generated a A folder with a Sample.swift file located in test, but these files are not linked to my Xcode project yet:
Now here is the script that I'm executing:
require 'xcodeproj'
project_path = '../TestCodeProjTest.xcodeproj'
project = Xcodeproj::Project.open(project_path)
file_group = project["TestCodeProjTest"]["test"]
file_group.new_file("#{project.project_dir}/TestCodeProjTest/test/A")
project.save()
This almost works fine, except that it creates a folder reference instead of a group, and it doesn't link it to my target:
Hence the content of Sample.swift is unreachable.
Its hard to achieve by bash. But really easy if you use Ruby and xcodeproj gem from Cocoapods
Consider you have file structure like
├── GeneratedFiles
│   └── Sample1.swift
├── MyProject
│   ├── AppDelegate.swift
│   ├── ... all other files
│   ├── SceneDelegate.swift
│   └── ViewController.swift
├── MyProject.xcodeproj
│   ├── project.pbxproj
│   ├── .....
└── add_file.rb
Then you can add files like
require 'xcodeproj'
project_path = 'MyProject.xcodeproj'
project = Xcodeproj::Project.open(project_path)
file_group = project["MyProject"]
file_group.new_file("../GeneratedFiles/Sample1.swift")
project.save()
UPDATE:
project["MyProject"] returns a file group which is a group named MyProject in the root of the project, you can select another group inside MyProject by file_group = project["MyProject"]["MyGroup"]
Then the generated file path should be either related to that group like file_group.new_file("../../GeneratedFiles/Sample1.swift") or full path like file_group.new_file("#{project.project_dir}/GeneratedFiles/Sample1.swift")
More details about Xcodeproj here

golang unknown revision module/vX.Y.Z and importing package properly

I have a golang application structure like this:
.
├── calc
│   ├── go.mod
│   ├── main.go
│   └── Makefile
├── go.mod
├── LICENSE
├── num
│   ├── go.mod
│   └── num.go
└── README.md
Where calc is an "application" where I'm importing the num package to add 2 numbers.
calc/go.mod
go 1.15
require github.com/github_username/goapp/num v0.2.1
num/go.mod
module github.com/github_username/goapp/num/v0.2.1
go 1.15
go.mod
module github.com/github_username/goapp/v0.2.1
go 1.15
When in /calc, and I run go run main.go, I get the following:
go: github.com/github_username/goapp/num#v0.2.1: reading github.com/github_username/goapp/num/num/go.mod at revision num/v0.2.1: unknown revision num/v0.2.1
What am I doing wrong? The github repo has the annotated tags.
For further context, I'm mimicking a production setup where we have six different mini golang services in folders such as calc, calc2, etc. where each "calc" service has a go.mod file.
module github.com/github_username/goapp/num/v0.2.1
Is nonsense. The semver version tag "v0.2.1" does not belong into the module name.
(Note that for major versions > 1, e.g. 4.3.1, the major version becomes part of the name like in module github.com/user/proj/folder/v4).
And one more: There are no source code belonging to the root go.mod so this module makes no sense whatsoever.
You really should not make that many modules.
Are you working with private repositories?
if yes, then you need to configure OAuth authentication:
export GITHUB_TOKEN=MY_GITHUB_TOKEN
git config --global url."https://${GITHUB_TOKEN}:x-oauth-basic#github.com/".insteadOf "https://github.com/"
Now, if you don't have the necessity to use private repositories!
Turn your repositories to the public that will solve the problem too.

Easier way to require spec files in Sinatra

I have a spec_helper that looks like this:
require 'pry'
require 'helpers/data_helper.rb'
require 'distributer.rb'
require 'house_distributer.rb'
require 'accounting_service.rb'
require 'mixer_worker.rb'
require 'mixer.rb'
require 'transaction_service.rb'
ENV['RACK_ENV'] = 'test'
RSpec.configure do |config|
config.mock_with :rspec do |mocks|
mocks.verify_partial_doubles = true
end
config.warnings = true
config.order = :random
end
and a folder structure that looks like this:
.
├── Gemfile
├── Gemfile.lock
├── README.md
├── app.rb
├── config.ru
├── lib
│   ├── accounting_service.rb
│   ├── distributer.rb
│   ├── house_distributer.rb
│   ├── mixer.rb
│   ├── mixer_worker.rb
│   └── transaction_service.rb
├── public
│   ├── css
│   │   └── add_coins.css
│   ├── images
│   │   └── bitcoin_dawg.jpg
│   └── javascripts
│   └── add_coins.js
├── spec
│   ├── helpers
│   │   └── data_helper.rb
│   ├── lib
│   │   ├── accounting_service_spec.rb
│   │   └── transaction_service_spec.rb
│   └── spec_helper.rb
└── views
└── add_coins.erb
This does not work:
Dir["lib/*.rb"].each {|file| require file }
[1] pry(main)> Dir["lib/*.rb"]
=> ["lib/house_distributer.rb", "lib/distributer.rb", "lib/mixer.rb", "lib/accounting_service.rb", "lib/mixer_worker.rb", "lib/transaction_service.rb"]
I get this error message:
/Users/jwan/.rbenv/versions/2.1.2/lib/ruby/2.1.0/rubygems/core_ext/kernel_require.rb:55:in `require': cannot load such file -- lib/house_distributer.rb (LoadError)
from /Users/jwan/.rbenv/versions/2.1.2/lib/ruby/2.1.0/rubygems/core_ext/kernel_require.rb:55:in `require'
What can I do to make this easier?
Also side note, distributer.rb has to be loaded before house_distributer.rb because of this:
class HouseDistributer < Distributer
end
Some explanation of max pleaner's answer...
When you write:
require 'lib/house_distributer.rb'
ruby looks for the file in the directories assigned to the $LOAD_PATH environment variable. $LOAD_PATH is an array of Strings, where each String is a path to a directory. The directories in the $LOAD_PATH array are searched in order, and the first match wins.
If $LOAD_PATH contains a directory called:
'/Users/7stud/ruby_programs'
Then the require statement above will look for a file with the absolute path:
'/Users/7stud/ruby_programs/lib/house_distributer.rb'
You can check which directories are in your $LOAD_PATH like this:
$ puts $LOAD_PATH
This is what I get:
/Users/7stud/.rvm/gems/ruby-2.4.0#global/gems/did_you_mean-1.1.0/lib
/Users/7stud/.rvm/rubies/ruby-2.4.0/lib/ruby/site_ruby/2.4.0
/Users/7stud/.rvm/rubies/ruby-2.4.0/lib/ruby/site_ruby/2.4.0/x86_64-darwin14
/Users/7stud/.rvm/rubies/ruby-2.4.0/lib/ruby/site_ruby
/Users/7stud/.rvm/rubies/ruby-2.4.0/lib/ruby/vendor_ruby/2.4.0
/Users/7stud/.rvm/rubies/ruby-2.4.0/lib/ruby/vendor_ruby/2.4.0/x86_64-darwin14
/Users/7stud/.rvm/rubies/ruby-2.4.0/lib/ruby/vendor_ruby
/Users/7stud/.rvm/rubies/ruby-2.4.0/lib/ruby/2.4.0
/Users/7stud/.rvm/rubies/ruby-2.4.0/lib/ruby/2.4.0/x86_64-darwin14
Obviously, your app's files are not in directories like those.
On the other hand, if you require a file whose path starts with a / or a . -- for instance:
require './lib/house_distributer.rb'
then ruby skips $LOAD_PATH, and in this case ruby looks for the file relative to the current working directory. Note however, that the current working directory may not be the directory containing the file with the require statement. For instance, if you execute your sinatra program from a different directory, say two levels up from the file containing the require statement, then the two levels up directory will be the current working directory, and ruby will look for the required file relative to the two levels up directory.
Enter require_relative. require_relative will look for the file relative to the path of the current file--not the current working directory.
As a result, you should probably never use require with a relative path and instead use require_relative.
Note that you can also programmatically add paths to $LOAD_PATH any time you want:
$LOAD_PATH << '/Users/7stud/ruby_programs'
And if a file called dog.rb is in that directory, I can require it like so:
require 'dog' #extension is unnecessary
Response to comment:
The simplest thing to do would be:
$LOAD_PATH << "/Users/jwan/Desktop/programming/interview_questions/gemini/‌​jobcoin_mixer/"
But in sinatra, settings.root is the path to your app directory, so do:
$LOAD_PATH.unshift setttings.root
That way you can move your app to another directory without changing anything.
Or, you can remove lib/ from the front of every path that Dir[] returned:
require 'pathname'
paths = [
"lib/house_distributer.rb",
"lib/distributer.rb",
"lib/mixer.rb",
"lib/accounting_service.rb",
]
new_paths = paths.map do |path|
pn = Pathname.new path
pn.relative_path_from(pn.parent).to_s
end
p new_paths
--output:--
["house_distributer.rb", "distributer.rb", "mixer.rb", "accounting_service.rb"]
The file not found is because you use "lib/*.rb" and not ./"lib/*.rb".
To ensure the dependencies are loaded in the correct order, you can do this:
move HouseDistributor to lib/distributor/house_distributor.rb
Require files like so:
Dir['./lib/**/*.rb']
.sort_by { |path| path.count("/") }
.each { |path| require path }
this uses **/*.rb to do a recursive search and will sort the files by the count of "/" (their depth) before requiring
Just a note of caution, if you are doing a recursive require, keep in mind that you actually do want to require all those files. For example if you are are using ActiveRecord and have a schema.rb file, you probably don't want to require that.
The other 2 answers are technically correct and address the goal but not the aim. In other words - why are you doing that in the first place? For instance, why do this:
Also side note, distributer.rb has to be loaded before house_distributer.rb because of this:
class HouseDistributer < Distributer
end
And not this?
require_relative "distributer.rb"
class HouseDistributer < Distributer
end
And in the tests (cough) sorry, the specs:
# spec/house_distributer_spec.rb
require 'spec_helper'
require_relative "../lib/house_distributer.rb"
# spec/transaction_service_spec.rb
require 'spec_helper'
require_relative "../lib/transaction_service.rb"
And since transaction_service.rb appears to need HouseDistributer from lib/house_distributer.rb…
# lib/transaction_service.rb
require_relative "house_distributer.rb"
The rule of thumb
If a file needs another to run then require (or require_relative) it in the file that requires it. Then you get:
A kind of natural sandboxing by only running what is needed (maybe Distributer works perfectly and HouseDistributer has the error because of a monkey patch - how will you know if you require files that aren't actually needed?)
No need to handle requires elsewhere.
No need (or much less need) to know the order of requires.
No need to fiddle about with the load path (which has been a dubious need ever since require_relative was introduced).
Changing the load path and then using require can break sandboxing and make specs work that should fail by loading a gem you have installed on your system but isn't defined as a dependency in the gemspec.
I'd add, use bundler's sandboxing too, to avoid further errors and let it handle your load path, e.g.
bundle install --binstubs --path=vendor (or vendor.noindex on a Mac) then:
bin/rspec and whatever commandline args you need.

Magento 2 Folder Structure Differ

I am new to Magento 2...and trying to learn CODEPOOL in Magento 2. This is a very basic question regarding Magento 2 Folder Structure.Magento 2 is differ from other previous version like Magento 1.9.1,1.9.0 ...
Magento 2 is significantly different from Magento 1.X and s not backward compatible as well.
In Magento 2,
All Custom Modules will go in app/code
Module Name will be something similar to app/code/[Company]/[Module]
View of the module (layout XMLs, Template .phtmls, Module Javascripts, LESS, CSS and all related files) will now go inside the Module Folder itself, making the module is standalone and independant
No More codepools. Core team has written their own modules for functionalities and core code lives in [MAGE_ROOT]/vendor/magento/. for example Catalog Module is now at [MAGE_ROOT]/vendor/magento/module-catalog having the module name Magento_Catalog
Even the Magento themes are now coming as Modules, look for [MAGE_ROOT]/vendor/magento/theme-frontend-luma or [MAGE_ROOT]/vendor/magento/theme-adminhtml-backend
Usage of Advanced Design Patterns and Features like Namespaces, Automatic Dependancy Injection, Static Content Generation
Some Used Technologies
LESS, jQuery, RequireJS, knockout.js and More
Varnish, Redis, Memcached
Solr
A complete list as per the documentation http://devdocs.magento.com/guides/v2.0/architecture/tech-stack.html
Good Tutorials to Follow
http://alanstorm.com/
https://www.ashsmith.io/
Give it a try. It's complex, more advanced. But worth learning...
Magento 2 all module reside inside app/code folder.
Inside app/code folder
/etc (main configuration folder module.xml)
/Setup (database table related file)
/Controller(action file)
/Model(Business logic)
/Helper (Miscellaneous data)
/Block (Block Template function file)
/view (phtml and layout file with css and js file)
/i18n (For translation language feature)
There are no core/community/local folder and those all folders are remove.
Magento use complete MVC pattern as following:-
1.complete module code resides in single folder:VendorName/ModuleName
2.No core/community/local folder
3.complete front end data (view data) resides into view folder.
4.Module register trough registration.php file.
5.dependency manage by composer.js file.
Magento 2 code structure is different from Magento 1. Code can be found under [MagentoRoot]/app/code and can also be installed under [MagentoRoot]/vendor directory using composer. Frontend Themes can be created under [MagentoRoot]/app/design/frontend and Admin Themes can be created under [MagentoRoot]/app/design/adminhtml
As far as concerned with Magento 2, the code pool structure of Magento 2 is different than Magento 1.
There are directories of modules where code found in Magento 2 as:
app/code
vendor
design/frontend
design/adminhtml
Every module follows the directory structure as VendorName/ModuleName inside the directory app/code, all the Code can be found under [MagentoRoot]/app/code, and can similar third-party modules also can be installed under /vendor directory using composer.json.
Please check the complete documentation:
https://developer.adobe.com/commerce/php/development/build/component-file-structure/
https://www.cloudways.com/blog/create-module-in-magento-2/
magento extension folder structure :
In Magneto 2 very easy to understand folder structure
Common directories
Following are some common module directories:
Block: contains PHP view classes as part of Model View Controller(MVC) vertical implementation of module logic.
Controller: contains PHP controller classes as part of MVC vertical implementation of module logic.
etc: contains configuration files; in particular, module.xml, which is required.
Model: contains PHP model classes as part of MVC vertical implementation of module logic.
Setup: contains classes for module database structure and data setup which are invoked when installing or upgrading.
Additional directories
Additional folders can be added for configuration and other ancillary functions for items like plugin-ins, localization, and layout files.
Api: contains any PHP classes exposed to the API.
i18n: contains localization files.
Plugin: contains any needed plug-ins.
view: contains view files, including static view files, design templates, email templates, and layout files
Theme file structure
A typical theme file structure can look like the following:
├── composer.json ├── etc │   └── view.xml ├── i18n │   └── en_US.csv ├── LICENSE_AFL.txt ├── LICENSE.txt ├── media │   └── preview.jpg ├── registration.php └── web ├── css │   ├── email.less │   ├── print.less │   ├── source │   │   ├── _actions-toolbar.less │   │   ├── _breadcrumbs.less │   │   ├── _buttons.less │   │   ├── components │   │   │   └── _modals_extend.less │   │   ├── _icons.less │   │   ├── _layout.less │   │   ├── _theme.less │   │   ├── _tooltips.less │   │   ├── _typography.less │   │   └── _variables.less │   ├── _styles.less │   ├── styles-l.less │   └── styles-m.less ├── images │   └── logo.svg └── js ├── navigation-menu.js ├── responsive.js └── theme.js
Common directories
Typical theme directories are:
etc: Contains configuration files such as the view.xml file which contains image configurations for all images and thumbnails.
i18n: Translation dictionaries, if any.
media: Theme preview images (screen capture of your theme) can be put in here.
web: Optional directory that contains static files organized into the following subdirectories:
css/source: Contains a theme’s less configuration files that invoke mixins for global elements from the Magento UI library, and the theme.less file that overrides the default variables values.
css/source/lib: Contains view files that override the UI library files stored in lib/web/css/source/lib.
fonts: The folder to place the different fonts for your theme.
images: Static images folder.
js: The folder for your JavaScript files.

Classes defined in CoffeeScript not found by Jasmine specs

I am building a backbone.js app on a Rails 3.1 back-end. I'm using CoffeeScript to write the backbone classes, and Jasmine (via jasmine-headless-webkit) for testing.
Given the following (partial) tree:
.
├── app
│   ├── assets
│   │   ├── javascripts
│   │   │   └── views
│   │   │   ├── avia_view.js.coffee
├── spec
│   ├── javascripts
│   │   └── views
│   │   └── avia_view_spec.js.coffee
... I would expect avia_view_spec.js.coffee to know about Avia.AviaView, which is defined in avia_view.js.coffee.
However, I get the following output from running bundle exec jasmine-headless-webkit:
Running Jasmine specs...
F
Avia.AviaView render creates a new MatricesView . (/home/duncan/avia/spec/javascripts/views/avia_view_spec.js.coffee:10)
ReferenceError: Can't find variable: Avia in /home/duncan/avia/spec/javascripts/views/avia_view_spec.js.coffee (line ~5)
ReferenceError: Can't find variable: Avia in /home/duncan/avia/spec/javascripts/views/avia_view_spec.js.coffee (line ~10)
My jasmine.yml contains the following:
src_files:
- public/javascripts/prototype.js
- public/javascripts/effects.js
- public/javascripts/controls.js
- public/javascripts/dragdrop.js
- public/javascripts/application.js
- public/javascripts/**/*.js
I think I need to tell Jasmine to load the contents of avia_view.js.coffee but I'm not entirely sure how. Adding an explicit reference in the src_files section in jasmine.yml doesn't seem to make a difference ...
Could someone please tell me what I'm doing wrong here? I suspect it's something simple ...
Without having seen to much of your code, I'd suspect it beacuse of CoffeeScript's function wrapping (docs). You need to ensure that all the symbols you want to use are exported to somewhere you can get at them (here is a thorough discussion about that).
Edit: here's another longish and theoretical but good documentation on this topic.
Try adding this to your avia_view.js.coffee
(exports ? this).Avia = Avia
See this for a detailed explanation.
Alternatively try this;
window.Avia = Avia
We encountered the same problem; I highly recommend JasmineRice

Resources